2019-05-18 06:36:33 -05:00
|
|
|
import { publishMainStream, publishGroupMessagingStream } from '../../../services/stream';
|
2019-02-04 23:14:23 -06:00
|
|
|
import { publishMessagingStream } from '../../../services/stream';
|
|
|
|
import { publishMessagingIndexStream } from '../../../services/stream';
|
2021-03-23 21:05:37 -05:00
|
|
|
import { User, IRemoteUser } from '../../../models/entities/user';
|
2019-04-07 07:50:36 -05:00
|
|
|
import { MessagingMessage } from '../../../models/entities/messaging-message';
|
2019-05-18 06:36:33 -05:00
|
|
|
import { MessagingMessages, UserGroupJoinings, Users } from '../../../models';
|
2019-04-07 07:50:36 -05:00
|
|
|
import { In } from 'typeorm';
|
2021-03-23 03:43:07 -05:00
|
|
|
import { IdentifiableError } from '@/misc/identifiable-error';
|
2019-05-18 06:36:33 -05:00
|
|
|
import { UserGroup } from '../../../models/entities/user-group';
|
2019-12-14 12:37:19 -06:00
|
|
|
import { toArray } from '../../../prelude/array';
|
|
|
|
import { renderReadActivity } from '../../../remote/activitypub/renderer/read';
|
|
|
|
import { renderActivity } from '../../../remote/activitypub/renderer';
|
|
|
|
import { deliver } from '../../../queue';
|
|
|
|
import orderedCollection from '../../../remote/activitypub/renderer/ordered-collection';
|
2017-06-12 12:12:30 -05:00
|
|
|
|
|
|
|
/**
|
2018-07-20 00:16:02 -05:00
|
|
|
* Mark messages as read
|
2017-06-12 12:12:30 -05:00
|
|
|
*/
|
2019-05-18 06:36:33 -05:00
|
|
|
export async function readUserMessagingMessage(
|
2019-04-07 07:50:36 -05:00
|
|
|
userId: User['id'],
|
|
|
|
otherpartyId: User['id'],
|
|
|
|
messageIds: MessagingMessage['id'][]
|
2019-05-18 06:36:33 -05:00
|
|
|
) {
|
2019-04-07 07:50:36 -05:00
|
|
|
if (messageIds.length === 0) return;
|
2017-06-12 12:12:30 -05:00
|
|
|
|
2019-05-18 06:36:33 -05:00
|
|
|
const messages = await MessagingMessages.find({
|
|
|
|
id: In(messageIds)
|
|
|
|
});
|
|
|
|
|
|
|
|
for (const message of messages) {
|
|
|
|
if (message.recipientId !== userId) {
|
|
|
|
throw new IdentifiableError('e140a4bf-49ce-4fb6-b67c-b78dadf6b52f', 'Access denied (user).');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-12 12:12:30 -05:00
|
|
|
// Update documents
|
2019-04-07 07:50:36 -05:00
|
|
|
await MessagingMessages.update({
|
|
|
|
id: In(messageIds),
|
2018-03-29 00:48:47 -05:00
|
|
|
userId: otherpartyId,
|
|
|
|
recipientId: userId,
|
|
|
|
isRead: false
|
2017-06-12 12:12:30 -05:00
|
|
|
}, {
|
2019-04-07 07:50:36 -05:00
|
|
|
isRead: true
|
|
|
|
});
|
2017-06-12 12:12:30 -05:00
|
|
|
|
|
|
|
// Publish event
|
2019-04-07 07:50:36 -05:00
|
|
|
publishMessagingStream(otherpartyId, userId, 'read', messageIds);
|
|
|
|
publishMessagingIndexStream(userId, 'read', messageIds);
|
2017-06-12 12:12:30 -05:00
|
|
|
|
2019-05-19 06:57:05 -05:00
|
|
|
if (!await Users.getHasUnreadMessagingMessage(userId)) {
|
2019-05-18 06:36:33 -05:00
|
|
|
// 全ての(いままで未読だった)自分宛てのメッセージを(これで)読みましたよというイベントを発行
|
|
|
|
publishMainStream(userId, 'readAllMessagingMessages');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mark messages as read
|
|
|
|
*/
|
|
|
|
export async function readGroupMessagingMessage(
|
|
|
|
userId: User['id'],
|
|
|
|
groupId: UserGroup['id'],
|
|
|
|
messageIds: MessagingMessage['id'][]
|
|
|
|
) {
|
|
|
|
if (messageIds.length === 0) return;
|
|
|
|
|
|
|
|
// check joined
|
|
|
|
const joining = await UserGroupJoinings.findOne({
|
|
|
|
userId: userId,
|
|
|
|
userGroupId: groupId
|
|
|
|
});
|
|
|
|
|
|
|
|
if (joining == null) {
|
|
|
|
throw new IdentifiableError('930a270c-714a-46b2-b776-ad27276dc569', 'Access denied (group).');
|
|
|
|
}
|
|
|
|
|
|
|
|
const messages = await MessagingMessages.find({
|
|
|
|
id: In(messageIds)
|
|
|
|
});
|
|
|
|
|
|
|
|
const reads = [];
|
|
|
|
|
|
|
|
for (const message of messages) {
|
|
|
|
if (message.userId === userId) continue;
|
|
|
|
if (message.reads.includes(userId)) continue;
|
|
|
|
|
|
|
|
// Update document
|
|
|
|
await MessagingMessages.createQueryBuilder().update()
|
|
|
|
.set({
|
|
|
|
reads: (() => `array_append("reads", '${joining.userId}')`) as any
|
|
|
|
})
|
|
|
|
.where('id = :id', { id: message.id })
|
|
|
|
.execute();
|
|
|
|
|
|
|
|
reads.push(message.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Publish event
|
|
|
|
publishGroupMessagingStream(groupId, 'read', {
|
|
|
|
ids: reads,
|
|
|
|
userId: userId
|
2019-04-07 07:50:36 -05:00
|
|
|
});
|
2019-05-18 06:36:33 -05:00
|
|
|
publishMessagingIndexStream(userId, 'read', reads);
|
2017-06-12 12:12:30 -05:00
|
|
|
|
2019-05-19 06:57:05 -05:00
|
|
|
if (!await Users.getHasUnreadMessagingMessage(userId)) {
|
2017-06-12 12:12:30 -05:00
|
|
|
// 全ての(いままで未読だった)自分宛てのメッセージを(これで)読みましたよというイベントを発行
|
2018-10-06 21:06:17 -05:00
|
|
|
publishMainStream(userId, 'readAllMessagingMessages');
|
2017-06-12 12:12:30 -05:00
|
|
|
}
|
2019-05-18 06:36:33 -05:00
|
|
|
}
|
2019-12-14 12:37:19 -06:00
|
|
|
|
2021-03-23 21:05:37 -05:00
|
|
|
export async function deliverReadActivity(user: { id: User['id']; host: null; }, recipient: IRemoteUser, messages: MessagingMessage | MessagingMessage[]) {
|
2019-12-14 12:37:19 -06:00
|
|
|
messages = toArray(messages).filter(x => x.uri);
|
|
|
|
const contents = messages.map(x => renderReadActivity(user, x));
|
|
|
|
|
|
|
|
if (contents.length > 1) {
|
|
|
|
const collection = orderedCollection(null, contents.length, undefined, undefined, contents);
|
|
|
|
deliver(user, renderActivity(collection), recipient.inbox);
|
|
|
|
} else {
|
|
|
|
for (const content of contents) {
|
|
|
|
deliver(user, renderActivity(content), recipient.inbox);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|