2019-04-07 21:50:36 +09:00
|
|
|
import { EntityRepository, Repository, In } from 'typeorm';
|
|
|
|
import { Note } from '../entities/note';
|
|
|
|
import { User } from '../entities/user';
|
2020-02-13 22:13:31 +09:00
|
|
|
import { Emojis, Users, PollVotes, DriveFiles, NoteReactions, Followings, Polls } from '..';
|
2019-04-13 01:43:22 +09:00
|
|
|
import { ensure } from '../../prelude/ensure';
|
2019-06-27 18:04:09 +09:00
|
|
|
import { SchemaType } from '../../misc/schema';
|
2019-04-23 22:35:26 +09:00
|
|
|
import { awaitAll } from '../../prelude/await-all';
|
2020-02-19 06:36:50 +09:00
|
|
|
import { convertLegacyReaction, convertLegacyReactions } from '../../misc/reaction-lib';
|
2020-03-06 22:51:50 +09:00
|
|
|
import { toString } from '../../mfm/toString';
|
|
|
|
import { parse } from '../../mfm/parse';
|
2019-04-23 22:35:26 +09:00
|
|
|
|
|
|
|
export type PackedNote = SchemaType<typeof packedNoteSchema>;
|
2019-04-07 21:50:36 +09:00
|
|
|
|
|
|
|
@EntityRepository(Note)
|
|
|
|
export class NoteRepository extends Repository<Note> {
|
|
|
|
public validateCw(x: string) {
|
|
|
|
return x.trim().length <= 100;
|
|
|
|
}
|
|
|
|
|
2019-04-23 22:35:26 +09:00
|
|
|
private async hideNote(packedNote: PackedNote, meId: User['id'] | null) {
|
2019-04-07 21:50:36 +09:00
|
|
|
let hide = false;
|
|
|
|
|
|
|
|
// visibility が specified かつ自分が指定されていなかったら非表示
|
2019-04-23 22:35:26 +09:00
|
|
|
if (packedNote.visibility === 'specified') {
|
2019-04-07 21:50:36 +09:00
|
|
|
if (meId == null) {
|
|
|
|
hide = true;
|
|
|
|
} else if (meId === packedNote.userId) {
|
|
|
|
hide = false;
|
|
|
|
} else {
|
|
|
|
// 指定されているかどうか
|
2019-04-23 22:35:26 +09:00
|
|
|
const specified = packedNote.visibleUserIds!.some((id: any) => meId === id);
|
2019-04-07 21:50:36 +09:00
|
|
|
|
|
|
|
if (specified) {
|
|
|
|
hide = false;
|
|
|
|
} else {
|
|
|
|
hide = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// visibility が followers かつ自分が投稿者のフォロワーでなかったら非表示
|
2020-04-04 08:46:54 +09:00
|
|
|
if (packedNote.visibility === 'followers') {
|
2019-04-07 21:50:36 +09:00
|
|
|
if (meId == null) {
|
|
|
|
hide = true;
|
|
|
|
} else if (meId === packedNote.userId) {
|
|
|
|
hide = false;
|
2019-04-23 22:35:26 +09:00
|
|
|
} else if (packedNote.reply && (meId === (packedNote.reply as PackedNote).userId)) {
|
2019-04-07 21:50:36 +09:00
|
|
|
// 自分の投稿に対するリプライ
|
|
|
|
hide = false;
|
2019-04-23 22:35:26 +09:00
|
|
|
} else if (packedNote.mentions && packedNote.mentions.some(id => meId === id)) {
|
2019-04-07 21:50:36 +09:00
|
|
|
// 自分へのメンション
|
|
|
|
hide = false;
|
|
|
|
} else {
|
|
|
|
// フォロワーかどうか
|
|
|
|
const following = await Followings.findOne({
|
|
|
|
followeeId: packedNote.userId,
|
|
|
|
followerId: meId
|
|
|
|
});
|
|
|
|
|
|
|
|
if (following == null) {
|
|
|
|
hide = true;
|
|
|
|
} else {
|
|
|
|
hide = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hide) {
|
2019-04-23 22:35:26 +09:00
|
|
|
packedNote.visibleUserIds = undefined;
|
2019-04-07 21:50:36 +09:00
|
|
|
packedNote.fileIds = [];
|
|
|
|
packedNote.files = [];
|
|
|
|
packedNote.text = null;
|
2019-04-23 22:35:26 +09:00
|
|
|
packedNote.poll = undefined;
|
2019-04-07 21:50:36 +09:00
|
|
|
packedNote.cw = null;
|
|
|
|
packedNote.isHidden = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public async pack(
|
|
|
|
src: Note['id'] | Note,
|
2019-04-13 01:43:22 +09:00
|
|
|
me?: User['id'] | User | null | undefined,
|
2019-04-07 21:50:36 +09:00
|
|
|
options?: {
|
|
|
|
detail?: boolean;
|
|
|
|
skipHide?: boolean;
|
|
|
|
}
|
2019-04-23 22:35:26 +09:00
|
|
|
): Promise<PackedNote> {
|
2019-04-07 21:50:36 +09:00
|
|
|
const opts = Object.assign({
|
|
|
|
detail: true,
|
|
|
|
skipHide: false
|
|
|
|
}, options);
|
|
|
|
|
|
|
|
const meId = me ? typeof me === 'string' ? me : me.id : null;
|
2019-04-13 01:43:22 +09:00
|
|
|
const note = typeof src === 'object' ? src : await this.findOne(src).then(ensure);
|
2019-04-07 21:50:36 +09:00
|
|
|
const host = note.userHost;
|
|
|
|
|
|
|
|
async function populatePoll() {
|
2019-04-17 00:34:49 +09:00
|
|
|
const poll = await Polls.findOne(note.id).then(ensure);
|
2019-04-07 21:50:36 +09:00
|
|
|
const choices = poll.choices.map(c => ({
|
|
|
|
text: c,
|
|
|
|
votes: poll.votes[poll.choices.indexOf(c)],
|
|
|
|
isVoted: false
|
|
|
|
}));
|
|
|
|
|
|
|
|
if (poll.multiple) {
|
|
|
|
const votes = await PollVotes.find({
|
2019-04-13 01:43:22 +09:00
|
|
|
userId: meId!,
|
2019-04-07 21:50:36 +09:00
|
|
|
noteId: note.id
|
|
|
|
});
|
|
|
|
|
|
|
|
const myChoices = votes.map(v => v.choice);
|
|
|
|
for (const myChoice of myChoices) {
|
|
|
|
choices[myChoice].isVoted = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const vote = await PollVotes.findOne({
|
2019-04-13 01:43:22 +09:00
|
|
|
userId: meId!,
|
2019-04-07 21:50:36 +09:00
|
|
|
noteId: note.id
|
|
|
|
});
|
|
|
|
|
|
|
|
if (vote) {
|
|
|
|
choices[vote.choice].isVoted = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
multiple: poll.multiple,
|
|
|
|
expiresAt: poll.expiresAt,
|
|
|
|
choices
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-01-07 12:28:20 +09:00
|
|
|
async function populateEmojis(emojiNames: string[], noteUserHost: string | null, reactionNames: string[]) {
|
|
|
|
const where = [] as {}[];
|
|
|
|
|
|
|
|
if (emojiNames?.length > 0) {
|
|
|
|
where.push({
|
|
|
|
name: In(emojiNames),
|
|
|
|
host: noteUserHost
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-01-09 14:37:48 +09:00
|
|
|
reactionNames = reactionNames?.filter(x => x.match(/^:[^:]+:$/)).map(x => x.replace(/:/g, ''));
|
|
|
|
|
2020-01-07 12:28:20 +09:00
|
|
|
if (reactionNames?.length > 0) {
|
|
|
|
where.push({
|
2020-01-09 14:37:48 +09:00
|
|
|
name: In(reactionNames),
|
2020-01-07 12:28:20 +09:00
|
|
|
host: null
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (where.length === 0) return [];
|
|
|
|
|
|
|
|
return Emojis.find({
|
|
|
|
where,
|
|
|
|
select: ['name', 'host', 'url', 'aliases']
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
async function populateMyReaction() {
|
|
|
|
const reaction = await NoteReactions.findOne({
|
2019-04-13 01:43:22 +09:00
|
|
|
userId: meId!,
|
2019-04-07 21:50:36 +09:00
|
|
|
noteId: note.id,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (reaction) {
|
2020-01-30 04:37:25 +09:00
|
|
|
return convertLegacyReaction(reaction.reaction);
|
2019-04-07 21:50:36 +09:00
|
|
|
}
|
|
|
|
|
2019-04-13 15:02:15 +09:00
|
|
|
return undefined;
|
2019-04-07 21:50:36 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
let text = note.text;
|
|
|
|
|
2020-04-02 21:59:14 +09:00
|
|
|
if (note.name && (note.url || note.uri)) {
|
|
|
|
text = `【${note.name}】\n${(note.text || '').trim()}\n\n${note.url || note.uri}`;
|
2019-04-07 21:50:36 +09:00
|
|
|
}
|
|
|
|
|
2019-04-23 22:35:26 +09:00
|
|
|
const packed = await awaitAll({
|
2019-04-07 21:50:36 +09:00
|
|
|
id: note.id,
|
2019-04-23 22:35:26 +09:00
|
|
|
createdAt: note.createdAt.toISOString(),
|
2019-04-07 21:50:36 +09:00
|
|
|
userId: note.userId,
|
|
|
|
user: Users.pack(note.user || note.userId, meId),
|
|
|
|
text: text,
|
|
|
|
cw: note.cw,
|
|
|
|
visibility: note.visibility,
|
2019-04-13 14:55:59 +09:00
|
|
|
localOnly: note.localOnly || undefined,
|
|
|
|
visibleUserIds: note.visibility === 'specified' ? note.visibleUserIds : undefined,
|
|
|
|
viaMobile: note.viaMobile || undefined,
|
2019-04-08 16:49:05 +09:00
|
|
|
renoteCount: note.renoteCount,
|
|
|
|
repliesCount: note.repliesCount,
|
2020-02-19 06:36:50 +09:00
|
|
|
reactions: convertLegacyReactions(note.reactions),
|
2019-06-07 20:40:05 +09:00
|
|
|
tags: note.tags.length > 0 ? note.tags : undefined,
|
2020-01-07 12:28:20 +09:00
|
|
|
emojis: populateEmojis(note.emojis, host, Object.keys(note.reactions)),
|
2019-04-07 21:50:36 +09:00
|
|
|
fileIds: note.fileIds,
|
|
|
|
files: DriveFiles.packMany(note.fileIds),
|
|
|
|
replyId: note.replyId,
|
|
|
|
renoteId: note.renoteId,
|
2019-04-18 01:05:40 +09:00
|
|
|
mentions: note.mentions.length > 0 ? note.mentions : undefined,
|
2019-04-17 07:25:34 +09:00
|
|
|
uri: note.uri || undefined,
|
2020-04-02 21:59:14 +09:00
|
|
|
url: note.url || undefined,
|
2020-02-18 19:05:11 +09:00
|
|
|
_featuredId_: (note as any)._featuredId_ || undefined,
|
|
|
|
_prId_: (note as any)._prId_ || undefined,
|
2019-04-07 21:50:36 +09:00
|
|
|
|
|
|
|
...(opts.detail ? {
|
|
|
|
reply: note.replyId ? this.pack(note.replyId, meId, {
|
|
|
|
detail: false
|
2019-04-13 14:55:59 +09:00
|
|
|
}) : undefined,
|
2019-04-07 21:50:36 +09:00
|
|
|
|
|
|
|
renote: note.renoteId ? this.pack(note.renoteId, meId, {
|
2019-04-08 17:04:37 +09:00
|
|
|
detail: true
|
2019-04-13 14:55:59 +09:00
|
|
|
}) : undefined,
|
2019-04-07 21:50:36 +09:00
|
|
|
|
2019-04-13 14:55:59 +09:00
|
|
|
poll: note.hasPoll ? populatePoll() : undefined,
|
2019-04-07 21:50:36 +09:00
|
|
|
|
|
|
|
...(meId ? {
|
|
|
|
myReaction: populateMyReaction()
|
|
|
|
} : {})
|
|
|
|
} : {})
|
|
|
|
});
|
|
|
|
|
|
|
|
if (packed.user.isCat && packed.text) {
|
2020-03-06 22:51:50 +09:00
|
|
|
const tokens = packed.text ? parse(packed.text) : [];
|
|
|
|
packed.text = toString(tokens, { doNyaize: true });
|
2019-04-07 21:50:36 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!opts.skipHide) {
|
|
|
|
await this.hideNote(packed, meId);
|
|
|
|
}
|
|
|
|
|
|
|
|
return packed;
|
|
|
|
}
|
2019-04-25 13:27:07 +09:00
|
|
|
|
|
|
|
public packMany(
|
|
|
|
notes: (Note['id'] | Note)[],
|
|
|
|
me?: User['id'] | User | null | undefined,
|
|
|
|
options?: {
|
|
|
|
detail?: boolean;
|
|
|
|
skipHide?: boolean;
|
|
|
|
}
|
|
|
|
) {
|
|
|
|
return Promise.all(notes.map(n => this.pack(n, me, options)));
|
|
|
|
}
|
2019-04-07 21:50:36 +09:00
|
|
|
}
|
2019-04-23 22:35:26 +09:00
|
|
|
|
|
|
|
export const packedNoteSchema = {
|
2019-06-27 18:04:09 +09:00
|
|
|
type: 'object' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 22:35:26 +09:00
|
|
|
properties: {
|
|
|
|
id: {
|
2019-06-27 18:04:09 +09:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 22:35:26 +09:00
|
|
|
format: 'id',
|
|
|
|
description: 'The unique identifier for this Note.',
|
|
|
|
example: 'xxxxxxxxxx',
|
|
|
|
},
|
|
|
|
createdAt: {
|
2019-06-27 18:04:09 +09:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 22:35:26 +09:00
|
|
|
format: 'date-time',
|
|
|
|
description: 'The date that the Note was created on Misskey.'
|
|
|
|
},
|
|
|
|
text: {
|
2019-06-27 18:04:09 +09:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: true as const,
|
2019-04-23 22:35:26 +09:00
|
|
|
},
|
|
|
|
cw: {
|
2019-06-27 18:04:09 +09:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: true as const, nullable: true as const,
|
2019-04-23 22:35:26 +09:00
|
|
|
},
|
|
|
|
userId: {
|
2019-06-27 18:04:09 +09:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 22:35:26 +09:00
|
|
|
format: 'id',
|
|
|
|
},
|
|
|
|
user: {
|
2019-06-27 18:04:09 +09:00
|
|
|
type: 'object' as const,
|
2019-04-23 22:35:26 +09:00
|
|
|
ref: 'User',
|
2019-06-27 18:04:09 +09:00
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 22:35:26 +09:00
|
|
|
},
|
|
|
|
replyId: {
|
2019-06-27 18:04:09 +09:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: true as const, nullable: true as const,
|
2019-04-23 22:35:26 +09:00
|
|
|
format: 'id',
|
|
|
|
example: 'xxxxxxxxxx',
|
|
|
|
},
|
|
|
|
renoteId: {
|
2019-06-27 18:04:09 +09:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: true as const, nullable: true as const,
|
2019-04-23 22:35:26 +09:00
|
|
|
format: 'id',
|
|
|
|
example: 'xxxxxxxxxx',
|
|
|
|
},
|
|
|
|
reply: {
|
2019-06-27 18:04:09 +09:00
|
|
|
type: 'object' as const,
|
|
|
|
optional: true as const, nullable: true as const,
|
2019-04-23 22:35:26 +09:00
|
|
|
ref: 'Note'
|
|
|
|
},
|
|
|
|
renote: {
|
2019-06-27 18:04:09 +09:00
|
|
|
type: 'object' as const,
|
|
|
|
optional: true as const, nullable: true as const,
|
2019-04-23 22:35:26 +09:00
|
|
|
ref: 'Note'
|
|
|
|
},
|
|
|
|
viaMobile: {
|
2019-06-27 18:04:09 +09:00
|
|
|
type: 'boolean' as const,
|
|
|
|
optional: true as const, nullable: false as const,
|
2019-04-23 22:35:26 +09:00
|
|
|
},
|
|
|
|
isHidden: {
|
2019-06-27 18:04:09 +09:00
|
|
|
type: 'boolean' as const,
|
|
|
|
optional: true as const, nullable: false as const,
|
2019-04-23 22:35:26 +09:00
|
|
|
},
|
|
|
|
visibility: {
|
2019-06-27 18:04:09 +09:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 22:35:26 +09:00
|
|
|
},
|
|
|
|
mentions: {
|
2019-06-27 18:04:09 +09:00
|
|
|
type: 'array' as const,
|
|
|
|
optional: true as const, nullable: false as const,
|
2019-04-23 22:35:26 +09:00
|
|
|
items: {
|
2019-06-27 18:04:09 +09:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 22:35:26 +09:00
|
|
|
format: 'id'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
visibleUserIds: {
|
2019-06-27 18:04:09 +09:00
|
|
|
type: 'array' as const,
|
|
|
|
optional: true as const, nullable: false as const,
|
2019-04-23 22:35:26 +09:00
|
|
|
items: {
|
2019-06-27 18:04:09 +09:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 22:35:26 +09:00
|
|
|
format: 'id'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
fileIds: {
|
2019-06-27 18:04:09 +09:00
|
|
|
type: 'array' as const,
|
|
|
|
optional: true as const, nullable: false as const,
|
2019-04-23 22:35:26 +09:00
|
|
|
items: {
|
2019-06-27 18:04:09 +09:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 22:35:26 +09:00
|
|
|
format: 'id'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
files: {
|
2019-06-27 18:04:09 +09:00
|
|
|
type: 'array' as const,
|
|
|
|
optional: true as const, nullable: false as const,
|
2019-04-23 22:35:26 +09:00
|
|
|
items: {
|
2019-06-27 18:04:09 +09:00
|
|
|
type: 'object' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 22:35:26 +09:00
|
|
|
ref: 'DriveFile'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
tags: {
|
2019-06-27 18:04:09 +09:00
|
|
|
type: 'array' as const,
|
|
|
|
optional: true as const, nullable: false as const,
|
2019-04-23 22:35:26 +09:00
|
|
|
items: {
|
2019-06-27 18:04:09 +09:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 22:35:26 +09:00
|
|
|
}
|
|
|
|
},
|
|
|
|
poll: {
|
2019-06-27 18:04:09 +09:00
|
|
|
type: 'object' as const,
|
|
|
|
optional: true as const, nullable: true as const,
|
2019-04-23 22:35:26 +09:00
|
|
|
},
|
2020-01-30 04:37:25 +09:00
|
|
|
|
2019-04-23 22:35:26 +09:00
|
|
|
},
|
|
|
|
};
|