Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import { EntityRepository, Repository, In } from 'typeorm';
import { Note } from '../entities/note';
import { User } from '../entities/user';
import { unique, concat } from '../../prelude/array';
import { nyaize } from '../../misc/nyaize';
import { Emojis, Users, Apps, PollVotes, DriveFiles, NoteReactions, Followings, Polls } from '..';
import rap from '@prezzemolo/rap';
@EntityRepository(Note)
export class NoteRepository extends Repository<Note> {
public validateCw(x: string) {
return x.trim().length <= 100;
}
private async hideNote(packedNote: any, meId: User['id']) {
let hide = false;
// visibility が specified かつ自分が指定されていなかったら非表示
if (packedNote.visibility == 'specified') {
if (meId == null) {
hide = true;
} else if (meId === packedNote.userId) {
hide = false;
} else {
// 指定されているかどうか
const specified = packedNote.visibleUserIds.some((id: any) => meId === id);
if (specified) {
hide = false;
} else {
hide = true;
}
}
}
// visibility が followers かつ自分が投稿者のフォロワーでなかったら非表示
if (packedNote.visibility == 'followers') {
if (meId == null) {
hide = true;
} else if (meId === packedNote.userId) {
hide = false;
} else if (packedNote.reply && (meId === packedNote.reply.userId)) {
// 自分の投稿に対するリプライ
hide = false;
} else if (packedNote.mentions && packedNote.mentions.some((id: any) => meId === id)) {
// 自分へのメンション
hide = false;
} else {
// フォロワーかどうか
const following = await Followings.findOne({
followeeId: packedNote.userId,
followerId: meId
});
if (following == null) {
hide = true;
} else {
hide = false;
}
}
}
if (hide) {
packedNote.visibleUserIds = null;
packedNote.fileIds = [];
packedNote.files = [];
packedNote.text = null;
packedNote.poll = null;
packedNote.cw = null;
packedNote.tags = [];
packedNote.geo = null;
packedNote.isHidden = true;
}
}
public packMany(
notes: (Note['id'] | Note)[],
me?: User['id'] | User,
options?: {
detail?: boolean;
skipHide?: boolean;
}
) {
return Promise.all(notes.map(n => this.pack(n, me, options)));
}
public async pack(
src: Note['id'] | Note,
me?: User['id'] | User,
options?: {
detail?: boolean;
skipHide?: boolean;
}
): Promise<Record<string, any>> {
const opts = Object.assign({
detail: true,
skipHide: false
}, options);
const meId = me ? typeof me === 'string' ? me : me.id : null;
const note = typeof src === 'object' ? src : await this.findOne(src);
const host = note.userHost;
async function populatePoll() {
const poll = await Polls.findOne({ noteId: note.id });
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({
userId: meId,
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({
userId: meId,
noteId: note.id
});
if (vote) {
choices[vote.choice].isVoted = true;
}
}
return {
multiple: poll.multiple,
expiresAt: poll.expiresAt,
choices
};
}
async function populateMyReaction() {
const reaction = await NoteReactions.findOne({
userId: meId,
noteId: note.id,
});
if (reaction) {
return reaction.reaction;
}
return null;
}
let text = note.text;
if (note.name) {
text = `【${note.name}】\n${note.text}`;
}
const reactionEmojis = unique(concat([note.emojis, Object.keys(note.reactions)]));
const packed = await rap({
id: note.id,
createdAt: note.createdAt,
app: note.appId ? Apps.pack(note.appId) : null,
userId: note.userId,
user: Users.pack(note.user || note.userId, meId),
text: text,
cw: note.cw,
visibility: note.visibility,
visibleUserIds: note.visibleUserIds,
viaMobile: note.viaMobile,
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
reactions: note.reactions,
emojis: reactionEmojis.length > 0 ? Emojis.find({
name: In(reactionEmojis),
host: host
}) : [],
tags: note.tags,
fileIds: note.fileIds,
files: DriveFiles.packMany(note.fileIds),
replyId: note.replyId,
renoteId: note.renoteId,
...(opts.detail ? {
reply: note.replyId ? this.pack(note.replyId, meId, {
detail: false
}) : null,
renote: note.renoteId ? this.pack(note.renoteId, meId, {
detail: false
}) : null,
poll: note.hasPoll ? populatePoll() : null,
...(meId ? {
myReaction: populateMyReaction()
} : {})
} : {})
});
if (packed.user.isCat && packed.text) {
packed.text = nyaize(packed.text);
}
if (!opts.skipHide) {
await this.hideNote(packed, meId);
}
return packed;
}
}