Skip to content

Commit

Permalink
deliver
Browse files Browse the repository at this point in the history
  • Loading branch information
mei23 committed Sep 28, 2023
1 parent 9590e98 commit 68e9788
Showing 1 changed file with 73 additions and 4 deletions.
77 changes: 73 additions & 4 deletions src/server/api/endpoints/notes/update.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import $ from 'cafy';
import * as mongo from 'mongodb';
import ID, { transform } from '../../../../misc/cafy-id';
import * as ms from 'ms';
import { length } from 'stringz';
Expand All @@ -9,6 +10,16 @@ import { ApiError } from '../../error';
import { getNote } from '../../common/getters';
import { publishNoteStream } from '../../../../services/stream';
import { oidEquals } from '../../../../prelude/oid';
import renderNote from '../../../../remote/activitypub/renderer/note';
import User, { IRemoteUser, isRemoteUser } from '../../../../models/user';
import DeliverManager from '../../../../remote/activitypub/deliver-manager';
import { deliverToRelays } from '../../../../services/relay';
import renderUpdate from '../../../../remote/activitypub/renderer/update';
import { renderActivity } from '../../../../remote/activitypub/renderer';
import { parseBasic } from '../../../../mfm/parse';
import { extractEmojis } from '../../../../mfm/extract-emojis';
import { extractHashtags } from '../../../../mfm/extract-hashtags';
import { extractMentionedUsers } from '../../../../services/note/create';

let maxNoteTextLength = 1000;

Expand Down Expand Up @@ -72,24 +83,82 @@ export const meta = {
};

export default define(meta, async (ps, user, app) => {
const note = await getNote(ps.noteId, user).catch(e => {
// check note
const origin = await getNote(ps.noteId, user).catch(e => {
if (e.id === '9725d0ce-ba28-4dde-95a7-2cbb2c15de24') throw new ApiError(meta.errors.noSuchNote);
throw e;
});

if (!oidEquals(note.userId, user._id)) {
// check note owner
if (!oidEquals(origin.userId, user._id)) {
throw new ApiError(meta.errors.noSuchNote);
}

// extract emojis, tags, mentions
const tokens = ps.text ? (parseBasic(ps.text) || []) : [];
const cwTokens = ps.cw ? (parseBasic(ps.cw) || []) : [];
const combinedTokens = tokens.concat(cwTokens);
const emojis = extractEmojis(combinedTokens);
const tags = extractHashtags(combinedTokens).filter(tag => Array.from(tag || '').length <= 128).splice(0, 64);

// Update
const updates = {
updatedAt: new Date(),
text: ps.text?.trim(),
cw: ps.cw ?? null,
emojis,
tags,
};

await Note.update({ _id: note._id }, {
await Note.update({ _id: origin._id }, {
$set: updates
});

publishNoteStream(note._id, 'updated', updates);
// Publish to streaming
publishNoteStream(origin._id, 'updated', updates);

// AP Deliver
(async () => {
if (user.noFederation) return;
if (origin.localOnly) return;

const note = await Note.findOne({ _id: origin._id });
if (!note) return;

const activity = renderActivity(renderUpdate(await renderNote(note), user));

const dm = new DeliverManager(user, activity);

// メンションされたリモートユーザーに配送
for (const u of (note.mentionedRemoteUsers || [])) {
dm.addDirectRecipe(await User.findOne({ _id: u }) as IRemoteUser);
}

// リプライ先
if (note._reply?.user.host) {
dm.addDirectRecipe(await User.findOne({ _id: note._reply.user }) as IRemoteUser);
}

// Renote/Quote先
if (note._renote?.user.host) {
dm.addDirectRecipe(await User.findOne({ _id: note._renote.user }) as IRemoteUser);
}

// フォロワーへ配送
if (['public', 'home', 'followers'].includes(note.visibility)) {
dm.addFollowersRecipe();
}

// リレーへ配送
if (['public'].includes(note.visibility) && !note.copyOnce) {
deliverToRelays(user, activity);
}

// リモートのみ配送
if (note.visibility === 'specified' && note.copyOnce) {
dm.addFollowersRecipe();
}

dm.execute();
})();
});

0 comments on commit 68e9788

Please sign in to comment.