strictNullChecks (#4666)
* wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip
This commit is contained in:
@@ -66,7 +66,7 @@ async function cancelRequest(follower: User, followee: User) {
|
||||
|
||||
// リモートからフォローリクエストを受けていたらReject送信
|
||||
if (Users.isRemoteUser(follower) && Users.isLocalUser(followee)) {
|
||||
const content = renderActivity(renderReject(renderFollow(follower, followee, request.requestId), followee));
|
||||
const content = renderActivity(renderReject(renderFollow(follower, followee, request.requestId!), followee));
|
||||
deliver(followee, content, follower.inbox);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -84,7 +84,7 @@ export default abstract class Chart<T extends Record<string, any>> {
|
||||
}
|
||||
}
|
||||
};
|
||||
flatColumns(schema.properties);
|
||||
flatColumns(schema.properties!);
|
||||
return columns;
|
||||
}
|
||||
|
||||
@@ -182,7 +182,7 @@ export default abstract class Chart<T extends Record<string, any>> {
|
||||
}
|
||||
|
||||
@autobind
|
||||
private getNewLog(latest?: T): T {
|
||||
private getNewLog(latest: T | null): T {
|
||||
const log = latest ? this.genNewLog(latest) : {};
|
||||
const flatColumns = (x: Obj, path?: string) => {
|
||||
for (const [k, v] of Object.entries(x)) {
|
||||
@@ -196,7 +196,7 @@ export default abstract class Chart<T extends Record<string, any>> {
|
||||
}
|
||||
}
|
||||
};
|
||||
flatColumns(this.schema.properties);
|
||||
flatColumns(this.schema.properties!);
|
||||
return log as T;
|
||||
}
|
||||
|
||||
@@ -213,7 +213,7 @@ export default abstract class Chart<T extends Record<string, any>> {
|
||||
}
|
||||
|
||||
@autobind
|
||||
private getLatestLog(span: Span, group: string = null): Promise<Log> {
|
||||
private getLatestLog(span: Span, group: string | null = null): Promise<Log | null> {
|
||||
return this.repository.findOne({
|
||||
group: group,
|
||||
span: span
|
||||
@@ -221,17 +221,17 @@ export default abstract class Chart<T extends Record<string, any>> {
|
||||
order: {
|
||||
date: -1
|
||||
}
|
||||
});
|
||||
}).then(x => x || null);
|
||||
}
|
||||
|
||||
@autobind
|
||||
private async getCurrentLog(span: Span, group: string = null): Promise<Log> {
|
||||
private async getCurrentLog(span: Span, group: string | null = null): Promise<Log> {
|
||||
const [y, m, d, h] = this.getCurrentDate();
|
||||
|
||||
const current =
|
||||
span == 'day' ? utc([y, m, d]) :
|
||||
span == 'hour' ? utc([y, m, d, h]) :
|
||||
null;
|
||||
null as never;
|
||||
|
||||
// 現在(今日または今のHour)のログ
|
||||
const currentLog = await this.repository.findOne({
|
||||
@@ -285,7 +285,7 @@ export default abstract class Chart<T extends Record<string, any>> {
|
||||
// 並列動作している他のチャートエンジンプロセスと処理が重なる場合がある
|
||||
// その場合は再度最も新しいログを持ってくる
|
||||
if (isDuplicateKeyValueError(e)) {
|
||||
log = await this.getLatestLog(span, group);
|
||||
log = await this.getLatestLog(span, group) as Log;
|
||||
} else {
|
||||
logger.error(e);
|
||||
throw e;
|
||||
@@ -296,17 +296,17 @@ export default abstract class Chart<T extends Record<string, any>> {
|
||||
}
|
||||
|
||||
@autobind
|
||||
protected commit(query: Record<string, Function>, group: string = null, uniqueKey?: string, uniqueValue?: string): Promise<any> {
|
||||
protected commit(query: Record<string, Function>, group: string | null = null, uniqueKey?: string, uniqueValue?: string): Promise<any> {
|
||||
const update = async (log: Log) => {
|
||||
// ユニークインクリメントの場合、指定のキーに指定の値が既に存在していたら弾く
|
||||
if (
|
||||
uniqueKey &&
|
||||
uniqueKey && log.unique &&
|
||||
log.unique[uniqueKey] &&
|
||||
log.unique[uniqueKey].includes(uniqueValue)
|
||||
) return;
|
||||
|
||||
// ユニークインクリメントの指定のキーに値を追加
|
||||
if (uniqueKey) {
|
||||
if (uniqueKey && log.unique) {
|
||||
if (log.unique[uniqueKey]) {
|
||||
const sql = `jsonb_set("unique", '{${uniqueKey}}', ("unique"->>'${uniqueKey}')::jsonb || '["${uniqueValue}"]'::jsonb)`;
|
||||
query['unique'] = () => sql;
|
||||
@@ -331,23 +331,23 @@ export default abstract class Chart<T extends Record<string, any>> {
|
||||
}
|
||||
|
||||
@autobind
|
||||
protected async inc(inc: DeepPartial<T>, group: string = null): Promise<void> {
|
||||
protected async inc(inc: DeepPartial<T>, group: string | null = null): Promise<void> {
|
||||
await this.commit(Chart.convertQuery(inc as any), group);
|
||||
}
|
||||
|
||||
@autobind
|
||||
protected async incIfUnique(inc: DeepPartial<T>, key: string, value: string, group: string = null): Promise<void> {
|
||||
protected async incIfUnique(inc: DeepPartial<T>, key: string, value: string, group: string | null = null): Promise<void> {
|
||||
await this.commit(Chart.convertQuery(inc as any), group, key, value);
|
||||
}
|
||||
|
||||
@autobind
|
||||
public async getChart(span: Span, range: number, group: string = null): Promise<ArrayValue<T>> {
|
||||
public async getChart(span: Span, range: number, group: string | null = null): Promise<ArrayValue<T>> {
|
||||
const [y, m, d, h] = this.getCurrentDate();
|
||||
|
||||
const gt =
|
||||
span == 'day' ? utc([y, m, d]).subtract(range, 'days') :
|
||||
span == 'hour' ? utc([y, m, d, h]).subtract(range, 'hours') :
|
||||
null;
|
||||
null as never;
|
||||
|
||||
// ログ取得
|
||||
let logs = await this.repository.find({
|
||||
@@ -404,7 +404,7 @@ export default abstract class Chart<T extends Record<string, any>> {
|
||||
const current =
|
||||
span == 'day' ? utc([y, m, d]).subtract(i, 'days') :
|
||||
span == 'hour' ? utc([y, m, d, h]).subtract(i, 'hours') :
|
||||
null;
|
||||
null as never;
|
||||
|
||||
const log = logs.find(l => utc(l.date * 1000).isSame(current));
|
||||
|
||||
@@ -452,8 +452,8 @@ export function convertLog(logSchema: Schema): Schema {
|
||||
type: 'number'
|
||||
};
|
||||
} else if (v.type === 'object') {
|
||||
for (const k of Object.keys(v.properties)) {
|
||||
v.properties[k] = convertLog(v.properties[k]);
|
||||
for (const k of Object.keys(v.properties!)) {
|
||||
v.properties![k] = convertLog(v.properties![k]);
|
||||
}
|
||||
}
|
||||
return v;
|
||||
|
||||
@@ -46,6 +46,7 @@ export async function createNotification(
|
||||
// 2秒経っても(今回作成した)通知が既読にならなかったら「未読の通知がありますよ」イベントを発行する
|
||||
setTimeout(async () => {
|
||||
const fresh = await Notifications.findOne(notification.id);
|
||||
if (fresh == null) return; // 既に削除されているかもしれない
|
||||
if (!fresh.isRead) {
|
||||
//#region ただしミュートしているユーザーからの通知なら無視
|
||||
const mutings = await Mutings.find({
|
||||
|
||||
@@ -55,10 +55,10 @@ async function save(file: DriveFile, path: string, name: string, type: string, h
|
||||
const url = `${ baseUrl }/${ key }`;
|
||||
|
||||
// for alts
|
||||
let webpublicKey: string = null;
|
||||
let webpublicUrl: string = null;
|
||||
let thumbnailKey: string = null;
|
||||
let thumbnailUrl: string = null;
|
||||
let webpublicKey: string | null = null;
|
||||
let webpublicUrl: string | null = null;
|
||||
let thumbnailKey: string | null = null;
|
||||
let thumbnailUrl: string | null = null;
|
||||
//#endregion
|
||||
|
||||
//#region Uploads
|
||||
@@ -106,8 +106,8 @@ async function save(file: DriveFile, path: string, name: string, type: string, h
|
||||
|
||||
const url = InternalStorage.saveFromPath(accessKey, path);
|
||||
|
||||
let thumbnailUrl: string;
|
||||
let webpublicUrl: string;
|
||||
let thumbnailUrl: string | null = null;
|
||||
let webpublicUrl: string | null = null;
|
||||
|
||||
if (alts.thumbnail) {
|
||||
thumbnailUrl = InternalStorage.saveFromBuffer(thumbnailAccessKey, alts.thumbnail.data);
|
||||
@@ -143,7 +143,7 @@ async function save(file: DriveFile, path: string, name: string, type: string, h
|
||||
*/
|
||||
export async function generateAlts(path: string, type: string, generateWeb: boolean) {
|
||||
// #region webpublic
|
||||
let webpublic: IImage;
|
||||
let webpublic: IImage | null = null;
|
||||
|
||||
if (generateWeb) {
|
||||
logger.info(`creating web image`);
|
||||
@@ -163,7 +163,7 @@ export async function generateAlts(path: string, type: string, generateWeb: bool
|
||||
// #endregion webpublic
|
||||
|
||||
// #region thumbnail
|
||||
let thumbnail: IImage;
|
||||
let thumbnail: IImage | null = null;
|
||||
|
||||
if (['image/jpeg', 'image/webp'].includes(type)) {
|
||||
thumbnail = await ConvertToJpeg(path, 498, 280);
|
||||
@@ -188,7 +188,7 @@ export async function generateAlts(path: string, type: string, generateWeb: bool
|
||||
* Upload to ObjectStorage
|
||||
*/
|
||||
async function upload(key: string, stream: fs.ReadStream | Buffer, type: string, filename?: string) {
|
||||
const minio = new Minio.Client(config.drive.config);
|
||||
const minio = new Minio.Client(config.drive!.config);
|
||||
|
||||
const metadata = {
|
||||
'Content-Type': type,
|
||||
@@ -197,7 +197,7 @@ async function upload(key: string, stream: fs.ReadStream | Buffer, type: string,
|
||||
|
||||
if (filename) metadata['Content-Disposition'] = contentDisposition('inline', filename);
|
||||
|
||||
await minio.putObject(config.drive.bucket, key, stream, null, metadata);
|
||||
await minio.putObject(config.drive!.bucket!, key, stream, undefined, metadata);
|
||||
}
|
||||
|
||||
async function deleteOldFile(user: IRemoteUser) {
|
||||
@@ -232,14 +232,14 @@ async function deleteOldFile(user: IRemoteUser) {
|
||||
export default async function(
|
||||
user: User,
|
||||
path: string,
|
||||
name: string = null,
|
||||
comment: string = null,
|
||||
name: string | null = null,
|
||||
comment: string | null = null,
|
||||
folderId: any = null,
|
||||
force: boolean = false,
|
||||
isLink: boolean = false,
|
||||
url: string = null,
|
||||
uri: string = null,
|
||||
sensitive: boolean = null
|
||||
url: string | null = null,
|
||||
uri: string | null = null,
|
||||
sensitive: boolean | null = null
|
||||
): Promise<DriveFile> {
|
||||
// Calc md5 hash
|
||||
const calcHash = new Promise<string>((res, rej) => {
|
||||
@@ -300,7 +300,7 @@ export default async function(
|
||||
throw 'no-free-space';
|
||||
} else {
|
||||
// (アバターまたはバナーを含まず)最も古いファイルを削除する
|
||||
deleteOldFile(user);
|
||||
deleteOldFile(user as IRemoteUser);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -378,7 +378,7 @@ export default async function(
|
||||
file.comment = comment;
|
||||
file.properties = properties;
|
||||
file.isLink = isLink;
|
||||
file.isSensitive = Users.isLocalUser(user) && profile.alwaysMarkNsfw ? true :
|
||||
file.isSensitive = Users.isLocalUser(user) && profile!.alwaysMarkNsfw ? true :
|
||||
(sensitive !== null && sensitive !== undefined)
|
||||
? sensitive
|
||||
: false;
|
||||
@@ -411,7 +411,7 @@ export default async function(
|
||||
file = await DriveFiles.findOne({
|
||||
uri: file.uri,
|
||||
userId: user.id
|
||||
});
|
||||
}) as DriveFile;
|
||||
} else {
|
||||
logger.error(e);
|
||||
throw e;
|
||||
|
||||
@@ -7,31 +7,31 @@ import { driveChart, perUserDriveChart, instanceChart } from '../chart';
|
||||
|
||||
export default async function(file: DriveFile, isExpired = false) {
|
||||
if (file.storedInternal) {
|
||||
InternalStorage.del(file.accessKey);
|
||||
InternalStorage.del(file.accessKey!);
|
||||
|
||||
if (file.thumbnailUrl) {
|
||||
InternalStorage.del(file.thumbnailAccessKey);
|
||||
InternalStorage.del(file.thumbnailAccessKey!);
|
||||
}
|
||||
|
||||
if (file.webpublicUrl) {
|
||||
InternalStorage.del(file.webpublicAccessKey);
|
||||
InternalStorage.del(file.webpublicAccessKey!);
|
||||
}
|
||||
} else if (!file.isLink) {
|
||||
const minio = new Minio.Client(config.drive.config);
|
||||
const minio = new Minio.Client(config.drive!.config);
|
||||
|
||||
await minio.removeObject(config.drive.bucket, file.accessKey);
|
||||
await minio.removeObject(config.drive!.bucket!, file.accessKey!);
|
||||
|
||||
if (file.thumbnailUrl) {
|
||||
await minio.removeObject(config.drive.bucket, file.thumbnailAccessKey);
|
||||
await minio.removeObject(config.drive!.bucket!, file.thumbnailAccessKey!);
|
||||
}
|
||||
|
||||
if (file.webpublicUrl) {
|
||||
await minio.removeObject(config.drive.bucket, file.webpublicAccessKey);
|
||||
await minio.removeObject(config.drive!.bucket!, file.webpublicAccessKey!);
|
||||
}
|
||||
}
|
||||
|
||||
// リモートファイル期限切れ削除後は直リンクにする
|
||||
if (isExpired && file.userHost !== null) {
|
||||
if (isExpired && file.userHost !== null && file.uri != null) {
|
||||
DriveFiles.update(file.id, {
|
||||
isLink: true,
|
||||
url: file.uri,
|
||||
|
||||
@@ -2,7 +2,7 @@ import * as sharp from 'sharp';
|
||||
|
||||
export type IImage = {
|
||||
data: Buffer;
|
||||
ext: string;
|
||||
ext: string | null;
|
||||
type: string;
|
||||
};
|
||||
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import * as URL from 'url';
|
||||
import create from './add-file';
|
||||
import { User } from '../../models/entities/user';
|
||||
import { driveLogger } from './logger';
|
||||
@@ -13,14 +12,14 @@ const logger = driveLogger.createSubLogger('downloader');
|
||||
export default async (
|
||||
url: string,
|
||||
user: User,
|
||||
folderId: DriveFolder['id'] = null,
|
||||
uri: string = null,
|
||||
folderId: DriveFolder['id'] | null = null,
|
||||
uri: string | null = null,
|
||||
sensitive = false,
|
||||
force = false,
|
||||
link = false
|
||||
): Promise<DriveFile> => {
|
||||
let name = URL.parse(url).pathname.split('/').pop();
|
||||
if (!DriveFiles.validateFileName(name)) {
|
||||
let name = new URL(url).pathname.split('/').pop() || null;
|
||||
if (name == null || !DriveFiles.validateFileName(name)) {
|
||||
name = null;
|
||||
}
|
||||
|
||||
@@ -50,6 +49,6 @@ export default async (
|
||||
if (error) {
|
||||
throw error;
|
||||
} else {
|
||||
return driveFile;
|
||||
return driveFile!;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -14,6 +14,7 @@ import { instanceChart, perUserFollowingChart } from '../chart';
|
||||
import { genId } from '../../misc/gen-id';
|
||||
import { createNotification } from '../create-notification';
|
||||
import { isDuplicateKeyValueError } from '../../misc/is-duplicate-key-value-error';
|
||||
import { ensure } from '../../prelude/ensure';
|
||||
|
||||
const logger = new Logger('following/create');
|
||||
|
||||
@@ -115,7 +116,7 @@ export default async function(follower: User, followee: User, requestId?: string
|
||||
if (blocked != null) throw new IdentifiableError('3338392a-f764-498d-8855-db939dcf8c48', 'blocked');
|
||||
}
|
||||
|
||||
const followeeProfile = await UserProfiles.findOne({ userId: followee.id });
|
||||
const followeeProfile = await UserProfiles.findOne(followee.id).then(ensure);
|
||||
|
||||
// フォロー対象が鍵アカウントである or
|
||||
// フォロワーがBotであり、フォロー対象がBotからのフォローに慎重である or
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import accept from './accept';
|
||||
import { User } from '../../../models/entities/user';
|
||||
import { FollowRequests, Users } from '../../../models';
|
||||
import { ensure } from '../../../prelude/ensure';
|
||||
|
||||
/**
|
||||
* 指定したユーザー宛てのフォローリクエストをすべて承認
|
||||
@@ -12,7 +13,7 @@ export default async function(user: User) {
|
||||
});
|
||||
|
||||
for (const request of requests) {
|
||||
const follower = await Users.findOne(request.followerId);
|
||||
const follower = await Users.findOne(request.followerId).then(ensure);
|
||||
accept(user, follower);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ export default async function(followee: User, follower: User) {
|
||||
await insertFollowingDoc(followee, follower);
|
||||
|
||||
if (Users.isRemoteUser(follower) && request) {
|
||||
const content = renderActivity(renderAccept(renderFollow(follower, followee, request.requestId), followee as ILocalUser));
|
||||
const content = renderActivity(renderAccept(renderFollow(follower, followee, request.requestId!), followee as ILocalUser));
|
||||
deliver(followee as ILocalUser, content, follower.inbox);
|
||||
}
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ export default async function(followee: User, follower: User) {
|
||||
followerId: follower.id
|
||||
});
|
||||
|
||||
const content = renderActivity(renderReject(renderFollow(follower, followee, request.requestId), followee as ILocalUser));
|
||||
const content = renderActivity(renderReject(renderFollow(follower, followee, request!.requestId!), followee as ILocalUser));
|
||||
deliver(followee as ILocalUser, content, follower.inbox);
|
||||
}
|
||||
|
||||
|
||||
@@ -77,9 +77,8 @@ export async function removePinned(user: User, noteId: Note['id']) {
|
||||
}
|
||||
|
||||
export async function deliverPinnedChange(userId: User['id'], noteId: Note['id'], isAddition: boolean) {
|
||||
const user = await Users.findOne({
|
||||
id: userId
|
||||
});
|
||||
const user = await Users.findOne(userId);
|
||||
if (user == null) throw 'user not found';
|
||||
|
||||
if (!Users.isLocalUser(user)) return;
|
||||
|
||||
@@ -108,14 +107,8 @@ async function CreateRemoteInboxes(user: ILocalUser): Promise<string[]> {
|
||||
const queue: string[] = [];
|
||||
|
||||
for (const following of followers) {
|
||||
const follower = {
|
||||
host: following.followerHost,
|
||||
inbox: following.followerInbox,
|
||||
sharedInbox: following.followerSharedInbox,
|
||||
};
|
||||
|
||||
if (follower.host !== null) {
|
||||
const inbox = follower.sharedInbox || follower.inbox;
|
||||
if (Followings.isRemoteFollower(following)) {
|
||||
const inbox = following.followerSharedInbox || following.followerInbox;
|
||||
if (!queue.includes(inbox)) queue.push(inbox);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,9 +6,8 @@ import { User } from '../../models/entities/user';
|
||||
import { renderPerson } from '../../remote/activitypub/renderer/person';
|
||||
|
||||
export async function publishToFollowers(userId: User['id']) {
|
||||
const user = await Users.findOne({
|
||||
id: userId
|
||||
});
|
||||
const user = await Users.findOne(userId);
|
||||
if (user == null) throw 'user not found';
|
||||
|
||||
const followers = await Followings.find({
|
||||
followeeId: user.id
|
||||
@@ -19,7 +18,7 @@ export async function publishToFollowers(userId: User['id']) {
|
||||
// フォロワーがリモートユーザーかつ投稿者がローカルユーザーならUpdateを配信
|
||||
if (Users.isLocalUser(user)) {
|
||||
for (const following of followers) {
|
||||
if (following.followerHost !== null) {
|
||||
if (Followings.isRemoteFollower(following)) {
|
||||
const inbox = following.followerSharedInbox || following.followerInbox;
|
||||
if (!queue.includes(inbox)) queue.push(inbox);
|
||||
}
|
||||
|
||||
@@ -9,14 +9,14 @@ import { genId } from '../misc/gen-id';
|
||||
|
||||
type Domain = {
|
||||
name: string;
|
||||
color: string;
|
||||
color?: string;
|
||||
};
|
||||
|
||||
type Level = 'error' | 'success' | 'warning' | 'debug' | 'info';
|
||||
|
||||
export default class Logger {
|
||||
private domain: Domain;
|
||||
private parentLogger: Logger;
|
||||
private parentLogger: Logger | null = null;
|
||||
private store: boolean;
|
||||
|
||||
constructor(domain: string, color?: string, store = true) {
|
||||
@@ -33,7 +33,7 @@ export default class Logger {
|
||||
return logger;
|
||||
}
|
||||
|
||||
private log(level: Level, message: string, data: Record<string, any>, important = false, subDomains: Domain[] = [], store = true): void {
|
||||
private log(level: Level, message: string, data?: Record<string, any> | null, important = false, subDomains: Domain[] = [], store = true): void {
|
||||
if (program.quiet) return;
|
||||
if (!this.store) store = false;
|
||||
|
||||
@@ -80,7 +80,7 @@ export default class Logger {
|
||||
}
|
||||
}
|
||||
|
||||
public error(x: string | Error, data?: Record<string, any>, important = false): void { // 実行を継続できない状況で使う
|
||||
public error(x: string | Error, data?: Record<string, any> | null, important = false): void { // 実行を継続できない状況で使う
|
||||
if (x instanceof Error) {
|
||||
data = data || {};
|
||||
data.e = x;
|
||||
@@ -90,21 +90,21 @@ export default class Logger {
|
||||
}
|
||||
}
|
||||
|
||||
public warn(message: string, data?: Record<string, any>, important = false): void { // 実行を継続できるが改善すべき状況で使う
|
||||
public warn(message: string, data?: Record<string, any> | null, important = false): void { // 実行を継続できるが改善すべき状況で使う
|
||||
this.log('warning', message, data, important);
|
||||
}
|
||||
|
||||
public succ(message: string, data?: Record<string, any>, important = false): void { // 何かに成功した状況で使う
|
||||
public succ(message: string, data?: Record<string, any> | null, important = false): void { // 何かに成功した状況で使う
|
||||
this.log('success', message, data, important);
|
||||
}
|
||||
|
||||
public debug(message: string, data?: Record<string, any>, important = false): void { // デバッグ用に使う(開発者に必要だが利用者に不要な情報)
|
||||
public debug(message: string, data?: Record<string, any> | null, important = false): void { // デバッグ用に使う(開発者に必要だが利用者に不要な情報)
|
||||
if (process.env.NODE_ENV != 'production' || program.verbose) {
|
||||
this.log('debug', message, data, important);
|
||||
}
|
||||
}
|
||||
|
||||
public info(message: string, data?: Record<string, any>, important = false): void { // それ以外
|
||||
public info(message: string, data?: Record<string, any> | null, important = false): void { // それ以外
|
||||
this.log('info', message, data, important);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ import { parse } from '../../mfm/parse';
|
||||
import { resolveUser } from '../../remote/resolve-user';
|
||||
import config from '../../config';
|
||||
import { updateHashtag } from '../update-hashtag';
|
||||
import { erase, concat } from '../../prelude/array';
|
||||
import { concat } from '../../prelude/array';
|
||||
import insertNoteUnread from './unread';
|
||||
import { registerOrFetchInstanceDoc } from '../register-or-fetch-instance-doc';
|
||||
import extractMentions from '../../misc/extract-mentions';
|
||||
@@ -27,6 +27,7 @@ import { notesChart, perUserNotesChart, activeUsersChart, instanceChart } from '
|
||||
import { Poll, IPoll } from '../../models/entities/poll';
|
||||
import { createNotification } from '../create-notification';
|
||||
import { isDuplicateKeyValueError } from '../../misc/is-duplicate-key-value-error';
|
||||
import { ensure } from '../../prelude/ensure';
|
||||
|
||||
type NotificationType = 'reply' | 'renote' | 'quote' | 'mention';
|
||||
|
||||
@@ -83,25 +84,25 @@ class NotificationManager {
|
||||
}
|
||||
|
||||
type Option = {
|
||||
createdAt?: Date;
|
||||
name?: string;
|
||||
text?: string;
|
||||
reply?: Note;
|
||||
renote?: Note;
|
||||
files?: DriveFile[];
|
||||
geo?: any;
|
||||
poll?: IPoll;
|
||||
viaMobile?: boolean;
|
||||
localOnly?: boolean;
|
||||
cw?: string;
|
||||
createdAt?: Date | null;
|
||||
name?: string | null;
|
||||
text?: string | null;
|
||||
reply?: Note | null;
|
||||
renote?: Note | null;
|
||||
files?: DriveFile[] | null;
|
||||
geo?: any | null;
|
||||
poll?: IPoll | null;
|
||||
viaMobile?: boolean | null;
|
||||
localOnly?: boolean | null;
|
||||
cw?: string | null;
|
||||
visibility?: string;
|
||||
visibleUsers?: User[];
|
||||
apMentions?: User[];
|
||||
apHashtags?: string[];
|
||||
apEmojis?: string[];
|
||||
questionUri?: string;
|
||||
uri?: string;
|
||||
app?: App;
|
||||
visibleUsers?: User[] | null;
|
||||
apMentions?: User[] | null;
|
||||
apHashtags?: string[] | null;
|
||||
apEmojis?: string[] | null;
|
||||
questionUri?: string | null;
|
||||
uri?: string | null;
|
||||
app?: App | null;
|
||||
};
|
||||
|
||||
export default async (user: User, data: Option, silent = false) => new Promise<Note>(async (res, rej) => {
|
||||
@@ -117,10 +118,6 @@ export default async (user: User, data: Option, silent = false) => new Promise<N
|
||||
data.visibility = 'home';
|
||||
}
|
||||
|
||||
if (data.visibleUsers) {
|
||||
data.visibleUsers = erase(null, data.visibleUsers);
|
||||
}
|
||||
|
||||
// Renote対象が「ホームまたは全体」以外の公開範囲ならreject
|
||||
if (data.renote && data.renote.visibility != 'public' && data.renote.visibility != 'home') {
|
||||
return rej('Renote target is not public or home');
|
||||
@@ -156,10 +153,10 @@ export default async (user: User, data: Option, silent = false) => new Promise<N
|
||||
|
||||
// Parse MFM if needed
|
||||
if (!tags || !emojis || !mentionedUsers) {
|
||||
const tokens = data.text ? parse(data.text) : [];
|
||||
const cwTokens = data.cw ? parse(data.cw) : [];
|
||||
const tokens = data.text ? parse(data.text)! : [];
|
||||
const cwTokens = data.cw ? parse(data.cw)! : [];
|
||||
const choiceTokens = data.poll && data.poll.choices
|
||||
? concat(data.poll.choices.map(choice => parse(choice)))
|
||||
? concat(data.poll.choices.map(choice => parse(choice)!))
|
||||
: [];
|
||||
|
||||
const combinedTokens = tokens.concat(cwTokens).concat(choiceTokens);
|
||||
@@ -173,19 +170,21 @@ export default async (user: User, data: Option, silent = false) => new Promise<N
|
||||
|
||||
tags = tags.filter(tag => tag.length <= 100);
|
||||
|
||||
if (data.reply && (user.id !== data.reply.userId) && !mentionedUsers.some(u => u.id === data.reply.userId)) {
|
||||
mentionedUsers.push(await Users.findOne(data.reply.userId));
|
||||
if (data.reply && (user.id !== data.reply.userId) && !mentionedUsers.some(u => u.id === data.reply!.userId)) {
|
||||
mentionedUsers.push(await Users.findOne(data.reply.userId).then(ensure));
|
||||
}
|
||||
|
||||
if (data.visibility == 'specified') {
|
||||
if (data.visibleUsers == null) throw 'invalid param';
|
||||
|
||||
for (const u of data.visibleUsers) {
|
||||
if (!mentionedUsers.some(x => x.id === u.id)) {
|
||||
mentionedUsers.push(u);
|
||||
}
|
||||
}
|
||||
|
||||
if (data.reply && !data.visibleUsers.some(x => x.id === data.reply.userId)) {
|
||||
data.visibleUsers.push(await Users.findOne(data.reply.userId));
|
||||
if (data.reply && !data.visibleUsers.some(x => x.id === data.reply!.userId)) {
|
||||
data.visibleUsers.push(await Users.findOne(data.reply.userId).then(ensure));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -215,6 +214,8 @@ export default async (user: User, data: Option, silent = false) => new Promise<N
|
||||
|
||||
// 未読通知を作成
|
||||
if (data.visibility == 'specified') {
|
||||
if (data.visibleUsers == null) throw 'invalid param';
|
||||
|
||||
for (const u of data.visibleUsers) {
|
||||
insertNoteUnread(u, note, true);
|
||||
}
|
||||
@@ -252,7 +253,7 @@ export default async (user: User, data: Option, silent = false) => new Promise<N
|
||||
deliverNoteToMentionedRemoteUsers(mentionedUsers, user, noteActivity);
|
||||
}
|
||||
|
||||
const profile = await UserProfiles.findOne({ userId: user.id });
|
||||
const profile = await UserProfiles.findOne({ userId: user.id }).then(ensure);
|
||||
|
||||
// If has in reply to note
|
||||
if (data.reply) {
|
||||
@@ -321,18 +322,18 @@ function incRenoteCount(renote: Note) {
|
||||
Notes.increment({ id: renote.id }, 'score', 1);
|
||||
}
|
||||
|
||||
async function publish(user: User, note: Note, reply: Note, renote: Note, noteActivity: any) {
|
||||
async function publish(user: User, note: Note, reply: Note | null | undefined, renote: Note | null | undefined, noteActivity: any) {
|
||||
if (Users.isLocalUser(user)) {
|
||||
// 投稿がリプライかつ投稿者がローカルユーザーかつリプライ先の投稿の投稿者がリモートユーザーなら配送
|
||||
if (reply && reply.userHost !== null) {
|
||||
Users.findOne(reply.userId).then(u => {
|
||||
Users.findOne(reply.userId).then(ensure).then(u => {
|
||||
deliver(user, noteActivity, u.inbox);
|
||||
});
|
||||
}
|
||||
|
||||
// 投稿がRenoteかつ投稿者がローカルユーザーかつRenote元の投稿の投稿者がリモートユーザーなら配送
|
||||
if (renote && renote.userHost !== null) {
|
||||
Users.findOne(renote.userId).then(u => {
|
||||
Users.findOne(renote.userId).then(ensure).then(u => {
|
||||
deliver(user, noteActivity, u.inbox);
|
||||
});
|
||||
}
|
||||
@@ -340,14 +341,14 @@ async function publish(user: User, note: Note, reply: Note, renote: Note, noteAc
|
||||
|
||||
if (['public', 'home', 'followers'].includes(note.visibility)) {
|
||||
// フォロワーに配信
|
||||
publishToFollowers(note, user, noteActivity, reply);
|
||||
publishToFollowers(note, user, noteActivity);
|
||||
}
|
||||
}
|
||||
|
||||
async function insertNote(user: User, data: Option, tags: string[], emojis: string[], mentionedUsers: User[]) {
|
||||
const insert = new Note({
|
||||
id: genId(data.createdAt),
|
||||
createdAt: data.createdAt,
|
||||
id: genId(data.createdAt!),
|
||||
createdAt: data.createdAt!,
|
||||
fileIds: data.files ? data.files.map(file => file.id) : [],
|
||||
replyId: data.reply ? data.reply.id : null,
|
||||
renoteId: data.renote ? data.renote.id : null,
|
||||
@@ -358,8 +359,8 @@ async function insertNote(user: User, data: Option, tags: string[], emojis: stri
|
||||
tags: tags.map(tag => tag.toLowerCase()),
|
||||
emojis,
|
||||
userId: user.id,
|
||||
viaMobile: data.viaMobile,
|
||||
localOnly: data.localOnly,
|
||||
viaMobile: data.viaMobile!,
|
||||
localOnly: data.localOnly!,
|
||||
geo: data.geo || null,
|
||||
appId: data.app ? data.app.id : null,
|
||||
visibility: data.visibility as any,
|
||||
@@ -401,10 +402,10 @@ async function insertNote(user: User, data: Option, tags: string[], emojis: stri
|
||||
|
||||
const poll = new Poll({
|
||||
noteId: note.id,
|
||||
choices: data.poll.choices,
|
||||
expiresAt: data.poll.expiresAt,
|
||||
multiple: data.poll.multiple,
|
||||
votes: new Array(data.poll.choices.length).fill(0),
|
||||
choices: data.poll!.choices,
|
||||
expiresAt: data.poll!.expiresAt,
|
||||
multiple: data.poll!.multiple,
|
||||
votes: new Array(data.poll!.choices.length).fill(0),
|
||||
noteVisibility: note.visibility,
|
||||
userId: user.id,
|
||||
userHost: user.host
|
||||
@@ -416,7 +417,7 @@ async function insertNote(user: User, data: Option, tags: string[], emojis: stri
|
||||
note = await Notes.save(insert);
|
||||
}
|
||||
|
||||
return note;
|
||||
return note!;
|
||||
} catch (e) {
|
||||
// duplicate key error
|
||||
if (isDuplicateKeyValueError(e)) {
|
||||
@@ -434,7 +435,7 @@ async function insertNote(user: User, data: Option, tags: string[], emojis: stri
|
||||
function index(note: Note) {
|
||||
if (note.text == null || config.elasticsearch == null) return;
|
||||
|
||||
es.index({
|
||||
es!.index({
|
||||
index: 'misskey',
|
||||
type: 'note',
|
||||
id: note.id.toString(),
|
||||
@@ -466,7 +467,7 @@ async function notifyToWatchersOfReplyee(reply: Note, user: User, nm: Notificati
|
||||
}
|
||||
}
|
||||
|
||||
async function publishToFollowers(note: Note, user: User, noteActivity: any, reply: Note) {
|
||||
async function publishToFollowers(note: Note, user: User, noteActivity: any) {
|
||||
const followers = await Followings.find({
|
||||
followeeId: note.userId
|
||||
});
|
||||
@@ -474,7 +475,7 @@ async function publishToFollowers(note: Note, user: User, noteActivity: any, rep
|
||||
const queue: string[] = [];
|
||||
|
||||
for (const following of followers) {
|
||||
if (following.followerHost !== null) {
|
||||
if (Followings.isRemoteFollower(following)) {
|
||||
// フォロワーがリモートユーザーかつ投稿者がローカルユーザーなら投稿を配信
|
||||
if (Users.isLocalUser(user)) {
|
||||
const inbox = following.followerSharedInbox || following.followerInbox;
|
||||
@@ -523,11 +524,9 @@ async function extractMentionedUsers(user: User, tokens: ReturnType<typeof parse
|
||||
|
||||
const mentions = extractMentions(tokens);
|
||||
|
||||
let mentionedUsers = await Promise.all(mentions.map(m =>
|
||||
let mentionedUsers = (await Promise.all(mentions.map(m =>
|
||||
resolveUser(m.username, m.host || user.host).catch(() => null)
|
||||
));
|
||||
|
||||
mentionedUsers = mentionedUsers.filter(x => x != null);
|
||||
))).filter(x => x != null) as User[];
|
||||
|
||||
// Drop duplicate users
|
||||
mentionedUsers = mentionedUsers.filter((u, i, self) =>
|
||||
|
||||
@@ -7,8 +7,10 @@ import { Note } from '../../../models/entities/note';
|
||||
|
||||
export async function deliverQuestionUpdate(noteId: Note['id']) {
|
||||
const note = await Notes.findOne(noteId);
|
||||
if (note == null) throw 'note not found';
|
||||
|
||||
const user = await Users.findOne(note.userId);
|
||||
if (user == null) throw 'note not found';
|
||||
|
||||
const followers = await Followings.find({
|
||||
followeeId: user.id
|
||||
@@ -19,13 +21,8 @@ export async function deliverQuestionUpdate(noteId: Note['id']) {
|
||||
// フォロワーがリモートユーザーかつ投稿者がローカルユーザーならUpdateを配信
|
||||
if (Users.isLocalUser(user)) {
|
||||
for (const following of followers) {
|
||||
const follower = {
|
||||
inbox: following.followerInbox,
|
||||
sharedInbox: following.followerSharedInbox
|
||||
};
|
||||
|
||||
if (following.followerHost !== null) {
|
||||
const inbox = follower.sharedInbox || follower.inbox;
|
||||
if (Followings.isRemoteFollower(following)) {
|
||||
const inbox = following.followerSharedInbox || following.followerInbox;
|
||||
if (!queue.includes(inbox)) queue.push(inbox);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,11 +7,13 @@ import { Not } from 'typeorm';
|
||||
import { genId } from '../../../misc/gen-id';
|
||||
import { createNotification } from '../../create-notification';
|
||||
|
||||
export default (user: User, note: Note, choice: number) => new Promise(async (res, rej) => {
|
||||
export default async function(user: User, note: Note, choice: number) {
|
||||
const poll = await Polls.findOne({ noteId: note.id });
|
||||
|
||||
if (poll == null) throw 'poll not found';
|
||||
|
||||
// Check whether is valid choice
|
||||
if (poll.choices[choice] == null) return rej('invalid choice param');
|
||||
if (poll.choices[choice] == null) throw new Error('invalid choice param');
|
||||
|
||||
// if already voted
|
||||
const exist = await PollVotes.find({
|
||||
@@ -21,10 +23,10 @@ export default (user: User, note: Note, choice: number) => new Promise(async (re
|
||||
|
||||
if (poll.multiple) {
|
||||
if (exist.some(x => x.choice === choice)) {
|
||||
return rej('already voted');
|
||||
throw new Error('already voted');
|
||||
}
|
||||
} else if (exist.length !== 0) {
|
||||
return rej('already voted');
|
||||
throw new Error('already voted');
|
||||
}
|
||||
|
||||
// Create vote
|
||||
@@ -36,8 +38,6 @@ export default (user: User, note: Note, choice: number) => new Promise(async (re
|
||||
choice: choice
|
||||
});
|
||||
|
||||
res();
|
||||
|
||||
// Increment votes count
|
||||
const index = choice + 1; // In SQL, array index is 1 based
|
||||
await Polls.query(`UPDATE poll SET votes[${index}] = votes[${index}] + 1 WHERE noteId = '${poll.noteId}'`);
|
||||
@@ -70,7 +70,7 @@ export default (user: User, note: Note, choice: number) => new Promise(async (re
|
||||
const profile = await UserProfiles.findOne({ userId: user.id });
|
||||
|
||||
// ローカルユーザーが投票した場合この投稿をWatchする
|
||||
if (Users.isLocalUser(user) && profile.autoWatch) {
|
||||
if (Users.isLocalUser(user) && profile!.autoWatch) {
|
||||
watch(user.id, note);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -82,7 +82,7 @@ export default async (user: User, note: Note, reaction: string) => {
|
||||
const profile = await UserProfiles.findOne({ userId: user.id });
|
||||
|
||||
// ユーザーがローカルユーザーかつ自動ウォッチ設定がオンならばこの投稿をWatchする
|
||||
if (Users.isLocalUser(user) && profile.autoWatch) {
|
||||
if (Users.isLocalUser(user) && profile!.autoWatch) {
|
||||
watch(user.id, note);
|
||||
}
|
||||
|
||||
@@ -91,7 +91,7 @@ export default async (user: User, note: Note, reaction: string) => {
|
||||
if (Users.isLocalUser(user) && note.userHost !== null) {
|
||||
const content = renderActivity(renderLike(user, note, reaction));
|
||||
Users.findOne(note.userId).then(u => {
|
||||
deliver(user, content, u.inbox);
|
||||
deliver(user, content, u!.inbox);
|
||||
});
|
||||
}
|
||||
//#endregion
|
||||
|
||||
@@ -42,7 +42,7 @@ export default async (user: User, note: Note) => {
|
||||
if (Users.isLocalUser(user) && (note.userHost !== null)) {
|
||||
const content = renderActivity(renderUndo(renderLike(user, note, exist.reaction), user));
|
||||
Users.findOne(note.userId).then(u => {
|
||||
deliver(user, content, u.inbox);
|
||||
deliver(user, content, u!.inbox);
|
||||
});
|
||||
}
|
||||
//#endregion
|
||||
|
||||
@@ -1,26 +1,17 @@
|
||||
import * as push from 'web-push';
|
||||
import config from '../config';
|
||||
import { SwSubscriptions } from '../models';
|
||||
import { Meta } from '../models/entities/meta';
|
||||
import fetchMeta from '../misc/fetch-meta';
|
||||
|
||||
let meta: Meta = null;
|
||||
|
||||
setInterval(() => {
|
||||
fetchMeta().then(m => {
|
||||
meta = m;
|
||||
|
||||
if (meta.enableServiceWorker) {
|
||||
// アプリケーションの連絡先と、サーバーサイドの鍵ペアの情報を登録
|
||||
push.setVapidDetails(config.url,
|
||||
meta.swPublicKey,
|
||||
meta.swPrivateKey);
|
||||
}
|
||||
});
|
||||
}, 3000);
|
||||
|
||||
export default async function(userId: string, type: string, body?: any) {
|
||||
if (!meta.enableServiceWorker) return;
|
||||
const meta = await fetchMeta();
|
||||
|
||||
if (!meta.enableServiceWorker || meta.swPublicKey == null || meta.swPrivateKey == null) return;
|
||||
|
||||
// アプリケーションの連絡先と、サーバーサイドの鍵ペアの情報を登録
|
||||
push.setVapidDetails(config.url,
|
||||
meta.swPublicKey,
|
||||
meta.swPrivateKey);
|
||||
|
||||
// Fetch
|
||||
const subscriptions = await SwSubscriptions.find({
|
||||
|
||||
@@ -5,8 +5,6 @@ import { genId } from '../misc/gen-id';
|
||||
import { toPuny } from '../misc/convert-host';
|
||||
|
||||
export async function registerOrFetchInstanceDoc(host: string): Promise<Instance> {
|
||||
if (host == null) return null;
|
||||
|
||||
host = toPuny(host);
|
||||
|
||||
const index = await Instances.findOne({ host });
|
||||
|
||||
@@ -6,7 +6,7 @@ import { UserList } from '../models/entities/user-list';
|
||||
import { ReversiGame } from '../models/entities/games/reversi/game';
|
||||
|
||||
class Publisher {
|
||||
private ev: Xev;
|
||||
private ev: Xev | null = null;
|
||||
|
||||
constructor() {
|
||||
// Redisがインストールされてないときはプロセス間通信を使う
|
||||
@@ -15,7 +15,7 @@ class Publisher {
|
||||
}
|
||||
}
|
||||
|
||||
private publish = (channel: string, type: string, value?: any): void => {
|
||||
private publish = (channel: string, type: string | null, value?: any): void => {
|
||||
const message = type == null ? value : value == null ?
|
||||
{ type: type, body: null } :
|
||||
{ type: type, body: value };
|
||||
@@ -23,7 +23,7 @@ class Publisher {
|
||||
if (this.ev) {
|
||||
this.ev.emit(channel, message);
|
||||
} else {
|
||||
redis.publish('misskey', JSON.stringify({
|
||||
redis!.publish('misskey', JSON.stringify({
|
||||
channel: channel,
|
||||
message: message
|
||||
}));
|
||||
|
||||
Reference in New Issue
Block a user