Co-authored-by: MeiMei <30769358+mei23@users.noreply.github.com>
Co-authored-by: Satsuki Yanagi <17376330+u1-liquid@users.noreply.github.com>
This commit is contained in:
syuilo 2020-01-30 04:37:25 +09:00 committed by GitHub
parent a5955c1123
commit f6154dc0af
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
871 changed files with 26140 additions and 71950 deletions

View file

@ -0,0 +1,54 @@
import { Antenna } from '../models/entities/antenna';
import { Note } from '../models/entities/note';
import { AntennaNotes, Mutings, Notes } from '../models';
import { genId } from '../misc/gen-id';
import shouldMuteThisNote from '../misc/should-mute-this-note';
import { ensure } from '../prelude/ensure';
import { publishAntennaStream, publishMainStream } from './stream';
import { User } from '../models/entities/user';
export async function addNoteToAntenna(antenna: Antenna, note: Note, noteUser: User) {
// 通知しない設定になっているか、自分自身の投稿なら既読にする
const read = !antenna.notify || (antenna.userId === noteUser.id);
AntennaNotes.save({
id: genId(),
antennaId: antenna.id,
noteId: note.id,
read: read,
});
publishAntennaStream(antenna.id, 'note', note);
if (!read) {
const mutings = await Mutings.find({
where: {
muterId: antenna.userId
},
select: ['muteeId']
});
const _note: Note = {
...note
};
if (note.replyId != null) {
_note.reply = await Notes.findOne(note.replyId).then(ensure);
}
if (note.renoteId != null) {
_note.renote = await Notes.findOne(note.renoteId).then(ensure);
}
if (shouldMuteThisNote(_note, mutings.map(x => x.muteeId))) {
return;
}
// 2秒経っても既読にならなかったら通知
setTimeout(async () => {
const unread = await AntennaNotes.findOne({ antennaId: antenna.id, read: false });
if (unread) {
publishMainStream(antenna.userId, 'unreadAntenna', antenna);
}
}, 2000);
}
}

View file

@ -5,6 +5,7 @@ import { genId } from '../misc/gen-id';
import { User } from '../models/entities/user';
import { Note } from '../models/entities/note';
import { Notification } from '../models/entities/notification';
import { FollowRequest } from '../models/entities/follow-request';
export async function createNotification(
notifieeId: User['id'],
@ -14,6 +15,7 @@ export async function createNotification(
noteId?: Note['id'];
reaction?: string;
choice?: number;
followRequestId?: FollowRequest['id'];
}
) {
if (notifieeId === notifierId) {
@ -33,6 +35,7 @@ export async function createNotification(
if (content.noteId) data.noteId = content.noteId;
if (content.reaction) data.reaction = content.reaction;
if (content.choice) data.choice = content.choice;
if (content.followRequestId) data.followRequestId = content.followRequestId;
}
// Create notification

View file

@ -45,11 +45,21 @@ export async function insertFollowingDoc(followee: User, follower: User) {
}
});
await FollowRequests.delete({
const req = await FollowRequests.findOne({
followeeId: followee.id,
followerId: follower.id
});
if (req) {
await FollowRequests.delete({
followeeId: followee.id,
followerId: follower.id
});
// 通知を作成
createNotification(follower.id, followee.id, 'followRequestAccepted');
}
if (alreadyFollowed) return;
//#region Increment counts

View file

@ -25,7 +25,7 @@ export default async function(follower: User, followee: User, requestId?: string
if (blocking != null) throw new Error('blocking');
if (blocked != null) throw new Error('blocked');
await FollowRequests.save({
const followRequest = await FollowRequests.save({
id: genId(),
createdAt: new Date(),
followerId: follower.id,
@ -50,7 +50,9 @@ export default async function(follower: User, followee: User, requestId?: string
}).then(packed => publishMainStream(followee.id, 'meUpdated', packed));
// 通知を作成
createNotification(followee.id, follower.id, 'receiveFollowRequest');
createNotification(followee.id, follower.id, 'receiveFollowRequest', {
followRequestId: followRequest.id
});
}
if (Users.isLocalUser(follower) && Users.isRemoteUser(followee)) {

View file

@ -17,7 +17,7 @@ import extractMentions from '../../misc/extract-mentions';
import extractEmojis from '../../misc/extract-emojis';
import extractHashtags from '../../misc/extract-hashtags';
import { Note, IMentionedRemoteUsers } from '../../models/entities/note';
import { Mutings, Users, NoteWatchings, Notes, Instances, UserProfiles } from '../../models';
import { Mutings, Users, NoteWatchings, Notes, Instances, UserProfiles, Antennas, Followings } from '../../models';
import { DriveFile } from '../../models/entities/drive-file';
import { App } from '../../models/entities/app';
import { Not, getConnection, In } from 'typeorm';
@ -28,6 +28,8 @@ 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';
import { checkHitAntenna } from '../../misc/check-hit-antenna';
import { addNoteToAntenna } from '../add-note-to-antenna';
type NotificationType = 'reply' | 'renote' | 'quote' | 'mention';
@ -90,7 +92,6 @@ type Option = {
reply?: Note | null;
renote?: Note | null;
files?: DriveFile[] | null;
geo?: any | null;
poll?: IPoll | null;
viaMobile?: boolean | null;
localOnly?: boolean | null;
@ -207,6 +208,23 @@ export default async (user: User, data: Option, silent = false) => new Promise<N
// Increment notes count (user)
incNotesCountOfUser(user);
// Antenna
Antennas.find().then(async antennas => {
const followings = await Followings.createQueryBuilder('following')
.andWhere(`following.followeeId = :userId`, { userId: note.userId })
.getMany();
const followers = followings.map(f => f.followerId);
for (const antenna of antennas) {
checkHitAntenna(antenna, note, user, followers).then(hit => {
if (hit) {
addNoteToAntenna(antenna, note, user);
}
});
}
});
if (data.reply) {
saveReply(data.reply, note);
}
@ -361,8 +379,6 @@ async function insertNote(user: User, data: Option, tags: string[], emojis: stri
userId: user.id,
viaMobile: data.viaMobile!,
localOnly: data.localOnly!,
geo: data.geo || null,
appId: data.app ? data.app.id : null,
visibility: data.visibility as any,
visibleUserIds: data.visibility == 'specified'
? data.visibleUsers

View file

@ -5,7 +5,6 @@ import { deliver } from '../../../queue';
import { renderActivity } from '../../../remote/activitypub/renderer';
import { IdentifiableError } from '../../../misc/identifiable-error';
import { toDbReaction } from '../../../misc/reaction-lib';
import { fetchMeta } from '../../../misc/fetch-meta';
import { User } from '../../../models/entities/user';
import { Note } from '../../../models/entities/note';
import { NoteReactions, Users, NoteWatchings, Notes, UserProfiles } from '../../../models';
@ -22,8 +21,7 @@ export default async (user: User, note: Note, reaction?: string) => {
throw new IdentifiableError('2d8e7297-1873-4c00-8404-792c68d7bef0', 'cannot react to my note');
}
const meta = await fetchMeta();
reaction = await toDbReaction(reaction, meta.enableEmojiReaction);
reaction = await toDbReaction(reaction);
// Create reaction
await NoteReactions.save({

View file

@ -1,7 +1,7 @@
import { publishMainStream } from '../stream';
import { Note } from '../../models/entities/note';
import { User } from '../../models/entities/user';
import { NoteUnreads } from '../../models';
import { NoteUnreads, Antennas, AntennaNotes, Users } from '../../models';
/**
* Mark a note as read
@ -17,27 +17,54 @@ export default (
});
// v11 TODO: https://github.com/typeorm/typeorm/issues/2415
//if (res.affected == 0) {
//if (res.affected === 0) {
// return;
//}
const count1 = await NoteUnreads.count({
userId: userId,
isSpecified: false
});
const [count1, count2] = await Promise.all([
NoteUnreads.count({
userId: userId,
isSpecified: false
}),
NoteUnreads.count({
userId: userId,
isSpecified: true
})
]);
const count2 = await NoteUnreads.count({
userId: userId,
isSpecified: true
});
if (count1 == 0) {
if (count1 === 0) {
// 全て既読になったイベントを発行
publishMainStream(userId, 'readAllUnreadMentions');
}
if (count2 == 0) {
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
});
const count = await AntennaNotes.count({
antennaId: antenna.id,
read: false
});
if (count === 0) {
publishMainStream(userId, 'readAntenna', antenna);
}
}));
Users.getHasUnreadAntenna(userId).then(unread => {
if (!unread) {
publishMainStream(userId, 'readAllAntennas');
}
})
});

View file

@ -5,6 +5,7 @@ import { UserList } from '../models/entities/user-list';
import { ReversiGame } from '../models/entities/games/reversi/game';
import { UserGroup } from '../models/entities/user-group';
import config from '../config';
import { Antenna } from '../models/entities/antenna';
class Publisher {
private publish = (channel: string, type: string | null, value?: any): void => {
@ -37,6 +38,10 @@ class Publisher {
this.publish(`userListStream:${listId}`, type, typeof value === 'undefined' ? null : value);
}
public publishAntennaStream = (antennaId: Antenna['id'], type: string, value?: any): void => {
this.publish(`antennaStream:${antennaId}`, type, typeof value === 'undefined' ? null : value);
}
public publishMessagingStream = (userId: User['id'], otherpartyId: User['id'], type: string, value?: any): void => {
this.publish(`messagingStream:${userId}-${otherpartyId}`, type, typeof value === 'undefined' ? null : value);
}
@ -61,10 +66,6 @@ class Publisher {
this.publish('notesStream', null, note);
}
public publishApLogStream = (log: any): void => {
this.publish('apLog', null, log);
}
public publishAdminStream = (userId: User['id'], type: string, value?: any): void => {
this.publish(`adminStream:${userId}`, type, typeof value === 'undefined' ? null : value);
}
@ -79,10 +80,10 @@ export const publishDriveStream = publisher.publishDriveStream;
export const publishNoteStream = publisher.publishNoteStream;
export const publishNotesStream = publisher.publishNotesStream;
export const publishUserListStream = publisher.publishUserListStream;
export const publishAntennaStream = publisher.publishAntennaStream;
export const publishMessagingStream = publisher.publishMessagingStream;
export const publishGroupMessagingStream = publisher.publishGroupMessagingStream;
export const publishMessagingIndexStream = publisher.publishMessagingIndexStream;
export const publishReversiStream = publisher.publishReversiStream;
export const publishReversiGameStream = publisher.publishReversiGameStream;
export const publishApLogStream = publisher.publishApLogStream;
export const publishAdminStream = publisher.publishAdminStream;