Merge branch 'develop' into sw-notification-action

This commit is contained in:
tamaina 2021-07-13 22:17:26 +09:00
commit 29dfc92b67
14 changed files with 266 additions and 135 deletions

View file

@ -30,7 +30,6 @@
"format": "gulp format" "format": "gulp format"
}, },
"resolutions": { "resolutions": {
"mfm-js/twemoji-parser": "13.1.x",
"chokidar": "^3.3.1", "chokidar": "^3.3.1",
"lodash": "^4.17.21" "lodash": "^4.17.21"
}, },

View file

@ -1,8 +1,8 @@
<template> <template>
<div class="mk-media-list"> <div class="hoawjimk">
<XBanner v-for="media in mediaList.filter(media => !previewable(media))" :media="media" :key="media.id"/> <XBanner v-for="media in mediaList.filter(media => !previewable(media))" :media="media" :key="media.id"/>
<div v-if="mediaList.filter(media => previewable(media)).length > 0" class="gird-container" ref="gridOuter"> <div v-if="mediaList.filter(media => previewable(media)).length > 0" class="gird-container">
<div :data-count="mediaList.filter(media => previewable(media)).length" :style="gridInnerStyle"> <div :data-count="mediaList.filter(media => previewable(media)).length">
<template v-for="media in mediaList"> <template v-for="media in mediaList">
<XVideo :video="media" :key="media.id" v-if="media.type.startsWith('video')"/> <XVideo :video="media" :key="media.id" v-if="media.type.startsWith('video')"/>
<XImage :image="media" :key="media.id" v-else-if="media.type.startsWith('image')" :raw="raw"/> <XImage :image="media" :key="media.id" v-else-if="media.type.startsWith('image')" :raw="raw"/>
@ -33,57 +33,16 @@ export default defineComponent({
default: false default: false
}, },
}, },
data() {
return {
gridInnerStyle: {},
sizeWaiting: false
}
},
mounted() {
this.size();
window.addEventListener('resize', this.size);
},
beforeUnmount() {
window.removeEventListener('resize', this.size);
},
activated() {
this.size();
},
methods: { methods: {
previewable(file) { previewable(file) {
return file.type.startsWith('video') || file.type.startsWith('image'); return file.type.startsWith('video') || file.type.startsWith('image');
}, },
size() {
// for Safari bug
if (this.sizeWaiting) return;
this.sizeWaiting = true;
window.requestAnimationFrame(() => {
this.sizeWaiting = false;
if (this.$refs.gridOuter) {
let height = 287;
const parent = this.$parent.$el;
if (this.$refs.gridOuter.clientHeight) {
height = this.$refs.gridOuter.clientHeight;
} else if (parent) {
height = parent.getBoundingClientRect().width * 9 / 16;
}
this.gridInnerStyle = { height: `${height}px` };
} else {
this.gridInnerStyle = {};
}
});
}
}, },
}); });
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>
.mk-media-list { .hoawjimk {
> .gird-container { > .gird-container {
position: relative; position: relative;
width: 100%; width: 100%;

View file

@ -9,7 +9,6 @@
<video <video
:poster="video.thumbnailUrl" :poster="video.thumbnailUrl"
:title="video.name" :title="video.name"
crossorigin="anonymous"
preload="none" preload="none"
controls controls
> >

View file

@ -10,7 +10,7 @@
</span> </span>
</li> </li>
</ul> </ul>
<p> <p v-if="!readOnly">
<span>{{ $t('_poll.totalVotes', { n: total }) }}</span> <span>{{ $t('_poll.totalVotes', { n: total }) }}</span>
<span> · </span> <span> · </span>
<a v-if="!closed && !isVoted" @click="toggleShowResult">{{ showResult ? $ts._poll.vote : $ts._poll.showResult }}</a> <a v-if="!closed && !isVoted" @click="toggleShowResult">{{ showResult ? $ts._poll.vote : $ts._poll.showResult }}</a>
@ -31,6 +31,11 @@ export default defineComponent({
note: { note: {
type: Object, type: Object,
required: true required: true
},
readOnly: {
type: Boolean,
required: false,
default: false,
} }
}, },
data() { data() {
@ -65,7 +70,7 @@ export default defineComponent({
} }
}, },
created() { created() {
this.showResult = this.isVoted; this.showResult = this.readOnly || this.isVoted;
if (this.note.poll.expiresAt) { if (this.note.poll.expiresAt) {
const update = () => { const update = () => {
@ -83,7 +88,7 @@ export default defineComponent({
this.showResult = !this.showResult; this.showResult = !this.showResult;
}, },
vote(id) { vote(id) {
if (this.closed || !this.poll.multiple && this.poll.choices.some(c => c.isVoted)) return; if (this.readOnly || this.closed || !this.poll.multiple && this.poll.choices.some(c => c.isVoted)) return;
os.api('notes/polls/vote', { os.api('notes/polls/vote', {
noteId: this.note.id, noteId: this.note.id,
choice: id choice: id

View file

@ -1,27 +1,44 @@
<template> <template>
<div class="civpbkhh"> <div class="civpbkhh">
<div class="scrollbox" ref="scroll" v-bind:class="{ scroll: isScrolling }">
<div v-for="note in notes" class="note"> <div v-for="note in notes" class="note">
<div class="content _panel"> <div class="content _panel">
{{ note.text }} <div class="body">
<MkA class="reply" v-if="note.replyId" :to="`/notes/${note.replyId}`"><i class="fas fa-reply"></i></MkA>
<Mfm v-if="note.text" :text="note.text" :author="note.user" :i="$i" :custom-emojis="note.emojis"/>
<MkA class="rp" v-if="note.renoteId" :to="`/notes/${note.renoteId}`">RN: ...</MkA>
</div>
<div v-if="note.files.length > 0" class="richcontent">
<XMediaList :media-list="note.files"/>
</div>
<div v-if="note.poll">
<XPoll :note="note" :readOnly="true" />
</div>
</div> </div>
<XReactionsViewer :note="note" ref="reactionsViewer"/> <XReactionsViewer :note="note" ref="reactionsViewer"/>
</div> </div>
</div> </div>
</div>
</template> </template>
<script lang="ts"> <script lang="ts">
import { defineComponent } from 'vue'; import { defineComponent } from 'vue';
import XReactionsViewer from '@client/components/reactions-viewer.vue'; import XReactionsViewer from '@client/components/reactions-viewer.vue';
import XMediaList from '@client/components/media-list.vue';
import XPoll from '@client/components/poll.vue';
import * as os from '@client/os'; import * as os from '@client/os';
export default defineComponent({ export default defineComponent({
components: { components: {
XReactionsViewer XReactionsViewer,
XMediaList,
XPoll
}, },
data() { data() {
return { return {
notes: [], notes: [],
isScrolling: false,
} }
}, },
@ -29,14 +46,40 @@ export default defineComponent({
os.api('notes/featured').then(notes => { os.api('notes/featured').then(notes => {
this.notes = notes; this.notes = notes;
}); });
},
updated() {
if (this.$refs.scroll.clientHeight > window.innerHeight) {
this.isScrolling = true;
}
} }
}); });
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>
@keyframes scroll {
0% {
transform: translate3d(0, 0, 0);
}
5% {
transform: translate3d(0, 0, 0);
}
75% {
transform: translate3d(0, calc(-100% + 90vh), 0);
}
90% {
transform: translate3d(0, calc(-100% + 90vh), 0);
}
}
.civpbkhh { .civpbkhh {
text-align: right; text-align: right;
> .scrollbox {
&.scroll {
animation: scroll 45s linear infinite;
}
> .note { > .note {
margin: 16px 0 16px auto; margin: 16px 0 16px auto;
@ -45,6 +88,11 @@ export default defineComponent({
margin: 0 0 0 auto; margin: 0 0 0 auto;
max-width: max-content; max-width: max-content;
border-radius: 16px; border-radius: 16px;
> .richcontent {
min-width: 250px;
}
}
} }
} }
} }

View file

@ -325,7 +325,6 @@ export class UserRepository extends Repository<User> {
//#region Validators //#region Validators
public validateLocalUsername = $.str.match(/^\w{1,20}$/); public validateLocalUsername = $.str.match(/^\w{1,20}$/);
public validateRemoteUsername = $.str.match(/^\w([\w-.]*\w)?$/);
public validatePassword = $.str.min(1); public validatePassword = $.str.min(1);
public validateName = $.str.min(1).max(50); public validateName = $.str.min(1).max(50);
public validateDescription = $.str.min(1).max(500); public validateDescription = $.str.min(1).max(500);

View file

@ -65,6 +65,11 @@ export default async (job: Bull.Job<InboxJobData>): Promise<string> => {
return `skip: failed to resolve user`; return `skip: failed to resolve user`;
} }
// publicKey がなくても終了
if (authUser.key == null) {
return `skip: failed to resolve user publicKey`;
}
// HTTP-Signatureの検証 // HTTP-Signatureの検証
const httpSignatureValidated = httpSignature.verifySignature(signature, authUser.key.keyPem); const httpSignatureValidated = httpSignature.verifySignature(signature, authUser.key.keyPem);
@ -89,6 +94,10 @@ export default async (job: Bull.Job<InboxJobData>): Promise<string> => {
return `skip: LD-Signatureのユーザーが取得できませんでした`; return `skip: LD-Signatureのユーザーが取得できませんでした`;
} }
if (authUser.key == null) {
return `skip: LD-SignatureのユーザーはpublicKeyを持っていませんでした`;
}
// LD-Signature検証 // LD-Signature検証
const ldSignature = new LdSignature(); const ldSignature = new LdSignature();
const verified = await ldSignature.verifyRsaSignature2017(activity, authUser.key.keyPem).catch(() => false); const verified = await ldSignature.verifyRsaSignature2017(activity, authUser.key.keyPem).catch(() => false);

View file

@ -98,7 +98,7 @@ export default class DbResolver {
if (user == null) return null; if (user == null) return null;
const key = await UserPublickeys.findOneOrFail(user.id); const key = await UserPublickeys.findOne(user.id);
return { return {
user, user,
@ -127,7 +127,7 @@ export default class DbResolver {
export type AuthUser = { export type AuthUser = {
user: IRemoteUser; user: IRemoteUser;
key: UserPublickey; key?: UserPublickey;
}; };
type UriParseResult = { type UriParseResult = {

View file

@ -1,10 +1,11 @@
import { URL } from 'url'; import { URL } from 'url';
import * as promiseLimit from 'promise-limit'; import * as promiseLimit from 'promise-limit';
import $, { Context } from 'cafy';
import config from '@/config'; import config from '@/config';
import Resolver from '../resolver'; import Resolver from '../resolver';
import { resolveImage } from './image'; import { resolveImage } from './image';
import { isCollectionOrOrderedCollection, isCollection, IPerson, getApId, getOneApHrefNullable, IObject, isPropertyValue, IApPropertyValue, getApType } from '../type'; import { isCollectionOrOrderedCollection, isCollection, IActor, getApId, getOneApHrefNullable, IObject, isPropertyValue, IApPropertyValue, getApType, isActor } from '../type';
import { fromHtml } from '../../../mfm/from-html'; import { fromHtml } from '../../../mfm/from-html';
import { htmlToMfm } from '../misc/html-to-mfm'; import { htmlToMfm } from '../misc/html-to-mfm';
import { resolveNote, extractEmojis } from './note'; import { resolveNote, extractEmojis } from './note';
@ -23,7 +24,6 @@ import { UserPublickey } from '../../../models/entities/user-publickey';
import { isDuplicateKeyValueError } from '@/misc/is-duplicate-key-value-error'; import { isDuplicateKeyValueError } from '@/misc/is-duplicate-key-value-error';
import { toPuny } from '@/misc/convert-host'; import { toPuny } from '@/misc/convert-host';
import { UserProfile } from '../../../models/entities/user-profile'; import { UserProfile } from '../../../models/entities/user-profile';
import { validActor } from '../../../remote/activitypub/type';
import { getConnection } from 'typeorm'; import { getConnection } from 'typeorm';
import { toArray } from '../../../prelude/array'; import { toArray } from '../../../prelude/array';
import { fetchInstanceMetadata } from '../../../services/fetch-instance-metadata'; import { fetchInstanceMetadata } from '../../../services/fetch-instance-metadata';
@ -32,58 +32,49 @@ import { normalizeForSearch } from '@/misc/normalize-for-search';
const logger = apLogger; const logger = apLogger;
/** /**
* Validate Person object * Validate and convert to actor object
* @param x Fetched person object * @param x Fetched object
* @param uri Fetch target URI * @param uri Fetch target URI
*/ */
function validatePerson(x: any, uri: string) { function validateActor(x: IObject, uri: string): IActor {
const expectHost = toPuny(new URL(uri).hostname); const expectHost = toPuny(new URL(uri).hostname);
if (x == null) { if (x == null) {
return new Error('invalid person: object is null'); throw new Error('invalid Actor: object is null');
} }
if (!validActor.includes(x.type)) { if (!isActor(x)) {
return new Error(`invalid person: object is not a person or service '${x.type}'`); throw new Error(`invalid Actor type '${x.type}'`);
} }
if (typeof x.preferredUsername !== 'string') { const validate = (name: string, value: any, validater: Context) => {
return new Error('invalid person: preferredUsername is not a string'); const e = validater.test(value);
} if (e) throw new Error(`invalid Actor: ${name} ${e.message}`);
};
if (typeof x.inbox !== 'string') { validate('id', x.id, $.str.min(1));
return new Error('invalid person: inbox is not a string'); validate('inbox', x.inbox, $.str.min(1));
} validate('preferredUsername', x.preferredUsername, $.str.min(1).max(128).match(/^\w([\w-.]*\w)?$/));
validate('name', x.name, $.optional.nullable.str.max(128));
validate('summary', x.summary, $.optional.nullable.str.max(2048));
if (!Users.validateRemoteUsername.ok(x.preferredUsername)) { const idHost = toPuny(new URL(x.id!).hostname);
return new Error('invalid person: invalid username');
}
if (x.name != null && x.name != '') {
if (!Users.validateName.ok(x.name)) {
return new Error('invalid person: invalid name');
}
}
if (typeof x.id !== 'string') {
return new Error('invalid person: id is not a string');
}
const idHost = toPuny(new URL(x.id).hostname);
if (idHost !== expectHost) { if (idHost !== expectHost) {
return new Error('invalid person: id has different host'); throw new Error('invalid Actor: id has different host');
} }
if (x.publicKey) {
if (typeof x.publicKey.id !== 'string') { if (typeof x.publicKey.id !== 'string') {
return new Error('invalid person: publicKey.id is not a string'); throw new Error('invalid Actor: publicKey.id is not a string');
} }
const publicKeyIdHost = toPuny(new URL(x.publicKey.id).hostname); const publicKeyIdHost = toPuny(new URL(x.publicKey.id).hostname);
if (publicKeyIdHost !== expectHost) { if (publicKeyIdHost !== expectHost) {
return new Error('invalid person: publicKey.id has different host'); throw new Error('invalid Actor: publicKey.id has different host');
}
} }
return null; return x;
} }
/** /**
@ -121,13 +112,7 @@ export async function createPerson(uri: string, resolver?: Resolver): Promise<Us
const object = await resolver.resolve(uri) as any; const object = await resolver.resolve(uri) as any;
const err = validatePerson(object, uri); const person = validateActor(object, uri);
if (err) {
throw err;
}
const person: IPerson = object;
logger.info(`Creating the Person: ${person.id}`); logger.info(`Creating the Person: ${person.id}`);
@ -178,11 +163,13 @@ export async function createPerson(uri: string, resolver?: Resolver): Promise<Us
userHost: host userHost: host
})); }));
if (person.publicKey) {
await transactionalEntityManager.save(new UserPublickey({ await transactionalEntityManager.save(new UserPublickey({
userId: user.id, userId: user.id,
keyId: person.publicKey.id, keyId: person.publicKey.id,
keyPem: person.publicKey.publicKeyPem keyPem: person.publicKey.publicKeyPem
})); }));
}
}); });
} catch (e) { } catch (e) {
// duplicate key error // duplicate key error
@ -294,13 +281,7 @@ export async function updatePerson(uri: string, resolver?: Resolver | null, hint
const object = hint || await resolver.resolve(uri) as any; const object = hint || await resolver.resolve(uri) as any;
const err = validatePerson(object, uri); const person = validateActor(object, uri);
if (err) {
throw err;
}
const person: IPerson = object;
logger.info(`Updating the Person: ${person.id}`); logger.info(`Updating the Person: ${person.id}`);
@ -358,10 +339,12 @@ export async function updatePerson(uri: string, resolver?: Resolver | null, hint
// Update user // Update user
await Users.update(exist.id, updates); await Users.update(exist.id, updates);
if (person.publicKey) {
await UserPublickeys.update({ userId: exist.id }, { await UserPublickeys.update({ userId: exist.id }, {
keyId: person.publicKey.id, keyId: person.publicKey.id,
keyPem: person.publicKey.publicKeyPem keyPem: person.publicKey.publicKeyPem
}); });
}
await UserProfiles.update({ userId: exist.id }, { await UserProfiles.update({ userId: exist.id }, {
url: getOneApHrefNullable(person.url), url: getOneApHrefNullable(person.url),

View file

@ -142,25 +142,25 @@ export const isTombstone = (object: IObject): object is ITombstone =>
export const validActor = ['Person', 'Service', 'Group', 'Organization', 'Application']; export const validActor = ['Person', 'Service', 'Group', 'Organization', 'Application'];
export const isActor = (object: IObject): object is IPerson => export const isActor = (object: IObject): object is IActor =>
validActor.includes(getApType(object)); validActor.includes(getApType(object));
export interface IPerson extends IObject { export interface IActor extends IObject {
type: 'Person' | 'Service' | 'Organization' | 'Group' | 'Application'; type: 'Person' | 'Service' | 'Organization' | 'Group' | 'Application';
name?: string; name?: string;
preferredUsername?: string; preferredUsername?: string;
manuallyApprovesFollowers?: boolean; manuallyApprovesFollowers?: boolean;
discoverable?: boolean; discoverable?: boolean;
inbox?: string; inbox: string;
sharedInbox?: string; // 後方互換性のため sharedInbox?: string; // 後方互換性のため
publicKey: { publicKey?: {
id: string; id: string;
publicKeyPem: string; publicKeyPem: string;
}; };
followers?: string | ICollection | IOrderedCollection; followers?: string | ICollection | IOrderedCollection;
following?: string | ICollection | IOrderedCollection; following?: string | ICollection | IOrderedCollection;
featured?: string | IOrderedCollection; featured?: string | IOrderedCollection;
outbox?: string | IOrderedCollection; outbox: string | IOrderedCollection;
endpoints?: { endpoints?: {
sharedInbox?: string; sharedInbox?: string;
}; };

View file

@ -5,9 +5,6 @@ import { Notification } from '../../../models/entities/notification';
import { Notifications, Users } from '../../../models'; import { Notifications, Users } from '../../../models';
import { In } from 'typeorm'; import { In } from 'typeorm';
/**
* Mark notifications as read
*/
export async function readNotification( export async function readNotification(
userId: User['id'], userId: User['id'],
notificationIds: Notification['id'][] notificationIds: Notification['id'][]
@ -20,6 +17,26 @@ export async function readNotification(
isRead: true isRead: true
}); });
post(userId);
}
export async function readNotificationByQuery(
userId: User['id'],
query: Record<string, any>
) {
// Update documents
await Notifications.update({
...query,
notifieeId: userId,
isRead: false
}, {
isRead: true
});
post(userId);
}
async function post(userId: User['id']) {
if (!await Users.getHasUnreadNotification(userId)) { if (!await Users.getHasUnreadNotification(userId)) {
// ユーザーのすべての通知が既読だったら、 // ユーザーのすべての通知が既読だったら、
// 全ての(いままで未読だった)通知を(これで)読みましたよというイベントを発行 // 全ての(いままで未読だった)通知を(これで)読みましたよというイベントを発行

View file

@ -7,6 +7,7 @@ import { Channel } from '../../models/entities/channel';
import { checkHitAntenna } from '@/misc/check-hit-antenna'; import { checkHitAntenna } from '@/misc/check-hit-antenna';
import { getAntennas } from '@/misc/antenna-cache'; import { getAntennas } from '@/misc/antenna-cache';
import { PackedNote } from '../../models/repositories/note'; import { PackedNote } from '../../models/repositories/note';
import { readNotificationByQuery } from '@/server/api/common/read-notification';
/** /**
* Mark notes as read * Mark notes as read
@ -96,6 +97,10 @@ export default async function(
publishMainStream(userId, 'readAllChannels'); publishMainStream(userId, 'readAllChannels');
} }
}); });
readNotificationByQuery(userId, {
noteId: In([...readMentions.map(n => n.id), ...readSpecifiedNotes.map(n => n.id)]),
});
} }
if (readAntennaNotes.length > 0) { if (readAntennaNotes.length > 0) {

73
test/activitypub.ts Normal file
View file

@ -0,0 +1,73 @@
/*
* Tests for ActivityPub
*
* How to run the tests:
* > npx cross-env TS_NODE_FILES=true TS_NODE_TRANSPILE_ONLY=true TS_NODE_PROJECT="./test/tsconfig.json" mocha test/activitypub.ts --require ts-node/register
*
* To specify test:
* > npx cross-env TS_NODE_FILES=true TS_NODE_TRANSPILE_ONLY=true TS_NODE_PROJECT="./test/tsconfig.json" npx mocha test/activitypub.ts --require ts-node/register -g 'test name'
*/
process.env.NODE_ENV = 'test';
import rndstr from 'rndstr';
import * as assert from 'assert';
import { initTestDb } from './utils';
describe('ActivityPub', () => {
before(async () => {
await initTestDb();
});
describe('Parse minimum object', () => {
const host = 'https://host1.test';
const preferredUsername = `${rndstr('A-Z', 4)}${rndstr('a-z', 4)}`;
const actorId = `${host}/users/${preferredUsername.toLowerCase()}`;
const actor = {
'@context': 'https://www.w3.org/ns/activitystreams',
id: actorId,
type: 'Person',
preferredUsername,
inbox: `${actorId}/inbox`,
outbox: `${actorId}/outbox`,
};
const post = {
'@context': 'https://www.w3.org/ns/activitystreams',
id: `${host}/users/${rndstr('0-9a-z', 8)}`,
type: 'Note',
attributedTo: actor.id,
to: 'https://www.w3.org/ns/activitystreams#Public',
content: 'あ',
};
it('Minimum Actor', async () => {
const { MockResolver } = await import('./misc/mock-resolver');
const { createPerson } = await import('../src/remote/activitypub/models/person');
const resolver = new MockResolver();
resolver._register(actor.id, actor);
const user = await createPerson(actor.id, resolver);
assert.deepStrictEqual(user.uri, actor.id);
assert.deepStrictEqual(user.username, actor.preferredUsername);
assert.deepStrictEqual(user.inbox, actor.inbox);
});
it('Minimum Note', async () => {
const { MockResolver } = await import('./misc/mock-resolver');
const { createNote } = await import('../src/remote/activitypub/models/note');
const resolver = new MockResolver();
resolver._register(actor.id, actor);
resolver._register(post.id, post);
const note = await createNote(post.id, resolver, true);
assert.deepStrictEqual(note?.uri, post.id);
assert.deepStrictEqual(note?.visibility, 'public');
assert.deepStrictEqual(note?.text, post.content);
});
});
});

View file

@ -0,0 +1,35 @@
import Resolver from '../../src/remote/activitypub/resolver';
import { IObject } from '../../src/remote/activitypub/type';
type MockResponse = {
type: string;
content: string;
};
export class MockResolver extends Resolver {
private _rs = new Map<string, MockResponse>();
public async _register(uri: string, content: string | Record<string, any>, type = 'application/activity+json') {
this._rs.set(uri, {
type,
content: typeof content === 'string' ? content : JSON.stringify(content)
});
}
public async resolve(value: string | IObject): Promise<IObject> {
if (typeof value !== 'string') return value;
const r = this._rs.get(value);
if (!r) {
throw {
name: `StatusError`,
statusCode: 404,
message: `Not registed for mock`
};
}
const object = JSON.parse(r.content);
return object;
}
}