Channel (#6621)
* wip * wip * wip * wip * wip * wip * wip * wip * wop * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * add notes * wip * wip * wip * wip * sound * wip * add kick_gaba2 * wip
This commit is contained in:
parent
122076e8ea
commit
9855405b89
70 changed files with 2191 additions and 184 deletions
|
|
@ -2,71 +2,104 @@ import { publishMainStream } from '../stream';
|
|||
import { Note } from '../../models/entities/note';
|
||||
import { User } from '../../models/entities/user';
|
||||
import { NoteUnreads, Antennas, AntennaNotes, Users } from '../../models';
|
||||
|
||||
// TODO: 状態が変化していない場合は各種イベントを送信しない
|
||||
import { Not, IsNull } from 'typeorm';
|
||||
|
||||
/**
|
||||
* Mark a note as read
|
||||
*/
|
||||
export default (
|
||||
export default async function(
|
||||
userId: User['id'],
|
||||
noteId: Note['id']
|
||||
) => new Promise<any>(async (resolve, reject) => {
|
||||
// Remove document
|
||||
/*const res = */await NoteUnreads.delete({
|
||||
userId: userId,
|
||||
noteId: noteId
|
||||
});
|
||||
|
||||
// v11 TODO: https://github.com/typeorm/typeorm/issues/2415
|
||||
//if (res.affected === 0) {
|
||||
// return;
|
||||
//}
|
||||
|
||||
const [count1, count2] = await Promise.all([
|
||||
NoteUnreads.count({
|
||||
) {
|
||||
async function careNoteUnreads() {
|
||||
const exist = await NoteUnreads.findOne({
|
||||
userId: userId,
|
||||
isSpecified: false
|
||||
}),
|
||||
NoteUnreads.count({
|
||||
userId: userId,
|
||||
isSpecified: true
|
||||
})
|
||||
]);
|
||||
|
||||
if (count1 === 0) {
|
||||
// 全て既読になったイベントを発行
|
||||
publishMainStream(userId, 'readAllUnreadMentions');
|
||||
}
|
||||
|
||||
if (count2 === 0) {
|
||||
// 全て既読になったイベントを発行
|
||||
publishMainStream(userId, 'readAllUnreadSpecifiedNotes');
|
||||
}
|
||||
|
||||
const antennas = await Antennas.find({ userId });
|
||||
|
||||
await Promise.all(antennas.map(async antenna => {
|
||||
await AntennaNotes.update({
|
||||
antennaId: antenna.id,
|
||||
noteId: noteId
|
||||
}, {
|
||||
read: true
|
||||
noteId: noteId,
|
||||
});
|
||||
|
||||
const count = await AntennaNotes.count({
|
||||
antennaId: antenna.id,
|
||||
read: false
|
||||
if (!exist) return;
|
||||
|
||||
// Remove the record
|
||||
await NoteUnreads.delete({
|
||||
userId: userId,
|
||||
noteId: noteId,
|
||||
});
|
||||
|
||||
if (count === 0) {
|
||||
publishMainStream(userId, 'readAntenna', antenna);
|
||||
if (exist.isMentioned) {
|
||||
NoteUnreads.count({
|
||||
userId: userId,
|
||||
isMentioned: true
|
||||
}).then(mentionsCount => {
|
||||
if (mentionsCount === 0) {
|
||||
// 全て既読になったイベントを発行
|
||||
publishMainStream(userId, 'readAllUnreadMentions');
|
||||
}
|
||||
});
|
||||
}
|
||||
}));
|
||||
|
||||
Users.getHasUnreadAntenna(userId).then(unread => {
|
||||
if (!unread) {
|
||||
publishMainStream(userId, 'readAllAntennas');
|
||||
if (exist.isSpecified) {
|
||||
NoteUnreads.count({
|
||||
userId: userId,
|
||||
isSpecified: true
|
||||
}).then(specifiedCount => {
|
||||
if (specifiedCount === 0) {
|
||||
// 全て既読になったイベントを発行
|
||||
publishMainStream(userId, 'readAllUnreadSpecifiedNotes');
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
if (exist.noteChannelId) {
|
||||
NoteUnreads.count({
|
||||
userId: userId,
|
||||
noteChannelId: Not(IsNull())
|
||||
}).then(channelNoteCount => {
|
||||
if (channelNoteCount === 0) {
|
||||
// 全て既読になったイベントを発行
|
||||
publishMainStream(userId, 'readAllChannels');
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async function careAntenna() {
|
||||
const beforeUnread = await Users.getHasUnreadAntenna(userId);
|
||||
if (!beforeUnread) return;
|
||||
|
||||
const antennas = await Antennas.find({ userId });
|
||||
|
||||
await Promise.all(antennas.map(async antenna => {
|
||||
const countBefore = await AntennaNotes.count({
|
||||
antennaId: antenna.id,
|
||||
read: false
|
||||
});
|
||||
|
||||
if (countBefore === 0) return;
|
||||
|
||||
await AntennaNotes.update({
|
||||
antennaId: antenna.id,
|
||||
noteId: noteId
|
||||
}, {
|
||||
read: true
|
||||
});
|
||||
|
||||
const countAfter = await AntennaNotes.count({
|
||||
antennaId: antenna.id,
|
||||
read: false
|
||||
});
|
||||
|
||||
if (countAfter === 0) {
|
||||
publishMainStream(userId, 'readAntenna', antenna);
|
||||
}
|
||||
}));
|
||||
|
||||
Users.getHasUnreadAntenna(userId).then(unread => {
|
||||
if (!unread) {
|
||||
publishMainStream(userId, 'readAllAntennas');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
careNoteUnreads();
|
||||
careAntenna();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue