merge: Link attributions (!1048)

View MR for information: https://activitypub.software/TransFem-org/Sharkey/-/merge_requests/1048

Approved-by: dakkar <dakkar@thenautilus.net>
Approved-by: Hazelnoot <acomputerdog@gmail.com>
This commit is contained in:
dakkar 2025-06-04 12:35:20 +00:00
commit e9a038b244
19 changed files with 252 additions and 13 deletions

12
locales/index.d.ts vendored
View file

@ -13157,6 +13157,18 @@ export interface Locale extends ILocale {
* Timeout in milliseconds for translation API requests.
*/
"translationTimeoutCaption": string;
/**
* Attribution Domains
*/
"attributionDomains": string;
/**
* A list of domains whose content can be attributed to you on link previews, separated by new-line. Any subdomain will also be valid. The following needs to be on the webpage:
*/
"attributionDomainsDescription": string;
/**
* Written by {user}
*/
"writtenBy": ParameterizedString<"user">;
/**
* Following (Pub)
*/

View file

@ -0,0 +1,16 @@
/*
* SPDX-FileCopyrightText: piuvas and other Sharkey contributors
* SPDX-License-Identifier: AGPL-3.0-only
*/
export class AddAttributionDomains1748096357260 {
name = 'AddAttributionDomains1748096357260'
async up(queryRunner) {
await queryRunner.query(`ALTER TABLE "user" ADD "attributionDomains" text array NOT NULL DEFAULT '{}'`);
}
async down(queryRunner) {
await queryRunner.query(`ALTER TABLE "user" DROP COLUMN "attributionDomains"`);
}
}

View file

@ -77,6 +77,7 @@ function generateDummyUser(override?: Partial<MiUser>): MiUser {
mandatoryCW: null,
rejectQuotes: false,
allowUnsignedFetch: 'staff',
attributionDomains: [],
...override,
};
}

View file

@ -613,6 +613,7 @@ export class ApRendererService {
enableRss: user.enableRss,
speakAsCat: user.speakAsCat,
attachment: attachment.length ? attachment : undefined,
attributionDomains: user.attributionDomains,
};
if (user.movedToUri) {

View file

@ -546,6 +546,10 @@ const extension_context_definition = {
featured: 'toot:featured',
discoverable: 'toot:discoverable',
indexable: 'toot:indexable',
attributionDomains: {
'@id': 'toot:attributionDomains',
'@type': '@id',
},
// schema
schema: 'http://schema.org#',
PropertyValue: 'schema:PropertyValue',

View file

@ -445,6 +445,7 @@ export class ApPersonService implements OnModuleInit, OnApplicationShutdown {
makeNotesFollowersOnlyBefore: (person as any).makeNotesFollowersOnlyBefore ?? null,
makeNotesHiddenBefore: (person as any).makeNotesHiddenBefore ?? null,
emojis,
attributionDomains: (Array.isArray(person.attributionDomains) && person.attributionDomains.every(x => typeof x === 'string')) ? person.attributionDomains : [],
})) as MiRemoteUser;
let _description: string | null = null;
@ -628,6 +629,7 @@ export class ApPersonService implements OnModuleInit, OnApplicationShutdown {
// We use "!== false" to handle incorrect types, missing / null values, and "default to true" logic.
hideOnlineStatus: person.hideOnlineStatus !== false,
isExplorable: person.discoverable !== false,
attributionDomains: (Array.isArray(person.attributionDomains) && person.attributionDomains.every(x => typeof x === 'string')) ? person.attributionDomains : [],
...(await this.resolveAvatarAndBanner(exist, person.icon, person.image, person.backgroundUrl).catch(() => ({}))),
} as Partial<MiRemoteUser> & Pick<MiRemoteUser, 'isBot' | 'isCat' | 'speakAsCat' | 'isLocked' | 'movedToUri' | 'alsoKnownAs' | 'isExplorable'>;

View file

@ -265,6 +265,7 @@ export interface IActor extends IObject {
enableRss?: boolean;
listenbrainz?: string;
backgroundUrl?: string;
attributionDomains?: string[];
}
export const isCollection = (object: IObject): object is ICollection =>

View file

@ -603,6 +603,7 @@ export class UserEntityService implements OnModuleInit {
enableRss: user.enableRss,
mandatoryCW: user.mandatoryCW,
rejectQuotes: user.rejectQuotes,
attributionDomains: user.attributionDomains,
isSilenced: user.isSilenced || this.roleService.getUserPolicies(user.id).then(r => !r.canPublicNote),
speakAsCat: user.speakAsCat ?? false,
approved: user.approved,

View file

@ -389,6 +389,12 @@ export class MiUser {
})
public allowUnsignedFetch: UserUnsignedFetchOption;
@Column('varchar', {
name: 'attributionDomains',
length: 128, array: true, default: '{}',
})
public attributionDomains: string[];
constructor(data: Partial<MiUser>) {
if (data == null) return;

View file

@ -236,6 +236,14 @@ export const packedUserLiteSchema = {
},
},
},
attributionDomains: {
type: 'array',
nullable: false, optional: false,
items: {
type: 'string',
nullable: false, optional: false,
},
},
},
} as const;

View file

@ -263,6 +263,9 @@ export const paramDef = {
enum: userUnsignedFetchOptions,
nullable: false,
},
attributionDomains: { type: 'array', items: {
type: 'string',
} },
},
} as const;
@ -373,6 +376,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
}
if (ps.mutedInstances !== undefined) profileUpdates.mutedInstances = ps.mutedInstances;
if (ps.notificationRecieveConfig !== undefined) profileUpdates.notificationRecieveConfig = ps.notificationRecieveConfig;
if (ps.attributionDomains !== undefined) updates.attributionDomains = ps.attributionDomains;
if (typeof ps.isLocked === 'boolean') updates.isLocked = ps.isLocked;
if (typeof ps.isExplorable === 'boolean') updates.isExplorable = ps.isExplorable;
if (typeof ps.hideOnlineStatus === 'boolean') updates.hideOnlineStatus = ps.hideOnlineStatus;
@ -663,7 +667,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
// these two methods need to be kept in sync with
// `ApRendererService.renderPerson`
private userNeedsPublishing(oldUser: MiLocalUser, newUser: Partial<MiUser>): boolean {
const basicFields: (keyof MiUser)[] = ['avatarId', 'bannerId', 'backgroundId', 'isBot', 'username', 'name', 'isLocked', 'isExplorable', 'isCat', 'noindex', 'speakAsCat', 'movedToUri', 'alsoKnownAs', 'hideOnlineStatus', 'enableRss', 'requireSigninToViewContents', 'makeNotesFollowersOnlyBefore', 'makeNotesHiddenBefore'];
const basicFields: (keyof MiUser)[] = ['avatarId', 'bannerId', 'backgroundId', 'isBot', 'username', 'name', 'isLocked', 'isExplorable', 'isCat', 'noindex', 'speakAsCat', 'movedToUri', 'alsoKnownAs', 'hideOnlineStatus', 'enableRss', 'requireSigninToViewContents', 'makeNotesFollowersOnlyBefore', 'makeNotesHiddenBefore', 'attributionDomains'];
for (const field of basicFields) {
if ((field in newUser) && oldUser[field] !== newUser[field]) {
return true;

View file

@ -20,6 +20,7 @@ import { RedisKVCache } from '@/misc/cache.js';
import { UtilityService } from '@/core/UtilityService.js';
import { ApDbResolverService } from '@/core/activitypub/ApDbResolverService.js';
import type { MiAccessToken, NotesRepository } from '@/models/_.js';
import { RemoteUserResolveService } from '@/core/RemoteUserResolveService.js';
import { ApUtilityService } from '@/core/activitypub/ApUtilityService.js';
import { ApRequestService } from '@/core/activitypub/ApRequestService.js';
import { SystemAccountService } from '@/core/SystemAccountService.js';
@ -30,10 +31,14 @@ import { BucketRateLimit, Keyed, sendRateLimitHeaders } from '@/misc/rate-limit-
import type { MiLocalUser } from '@/models/User.js';
import { getIpHash } from '@/misc/get-ip-hash.js';
import { isRetryableError } from '@/misc/is-retryable-error.js';
import * as Acct from '@/misc/acct.js';
import type { FastifyRequest, FastifyReply } from 'fastify';
export type LocalSummalyResult = SummalyResult & {
haveNoteLocally?: boolean;
linkAttribution?: {
userId: string,
}
};
// Increment this to invalidate cached previews after a major change.
@ -82,6 +87,7 @@ export class UrlPreviewService {
private readonly utilityService: UtilityService,
private readonly apUtilityService: ApUtilityService,
private readonly apDbResolverService: ApDbResolverService,
private readonly remoteUserResolveService: RemoteUserResolveService,
private readonly apRequestService: ApRequestService,
private readonly systemAccountService: SystemAccountService,
private readonly apNoteService: ApNoteService,
@ -206,6 +212,8 @@ export class UrlPreviewService {
}
}
await this.validateLinkAttribution(summary);
// Await this to avoid hammering redis when a bunch of URLs are fetched at once
await this.previewCache.set(cacheKey, summary);
@ -426,6 +434,30 @@ export class UrlPreviewService {
}
}
private async validateLinkAttribution(summary: LocalSummalyResult) {
if (!summary.fediverseCreator) return;
if (!URL.canParse(summary.url)) return;
const url = URL.parse(summary.url);
const acct = Acct.parse(summary.fediverseCreator);
if (acct.host?.toLowerCase() === this.config.host) {
acct.host = null;
}
try {
const user = await this.remoteUserResolveService.resolveUser(acct.username, acct.host);
const attributionDomains = user.attributionDomains;
if (attributionDomains.some(x => `.${url?.host.toLowerCase()}`.endsWith(`.${x}`))) {
summary.linkAttribution = {
userId: user.id,
};
}
} catch {
this.logger.debug('User not found: ' + summary.fediverseCreator);
}
}
// Adapted from ApiCallService
private async checkFetchPermissions(auth: AuthArray, reply: FastifyReply): Promise<boolean> {
const [user, app] = auth;

View file

@ -65,6 +65,17 @@ SPDX-License-Identifier: AGPL-3.0-only
</footer>
</article>
</component>
<I18n v-if="attributionUser" :src="i18n.ts.writtenBy" :class="$style.linkAttribution" tag="p">
<template #user>
<MkA v-user-preview="attributionUser.id" :to="userPage(attributionUser)">
<MkAvatar :class="$style.linkAttributionIcon" :user="attributionUser"/>
<MkUserName :user="attributionUser" style="color: var(--MI_THEME-accent)"/>
</MkA>
</template>
</I18n>
<p v-else-if="linkAttribution" :class="$style.linkAttribution"><MkEllipsis/></p>
<template v-if="showActions">
<div v-if="tweetId" :class="$style.action">
<MkButton :small="true" inline @click="tweetExpanded = true">
@ -106,6 +117,7 @@ import { misskeyApi } from '@/utility/misskey-api.js';
import { warningExternalWebsite } from '@/utility/warning-external-website.js';
import DynamicNoteSimple from '@/components/DynamicNoteSimple.vue';
import { $i } from '@/i';
import { userPage } from '@/filters/user.js';
type SummalyResult = Awaited<ReturnType<typeof summaly>>;
@ -146,6 +158,10 @@ const player = ref<SummalyResult['player']>({
height: null,
allow: [],
});
const linkAttribution = ref<{
userId: string,
} | null>(null);
const attributionUser = ref<Misskey.entities.User | null>(null);
const playerEnabled = ref(false);
const tweetId = ref<string | null>(null);
const tweetExpanded = ref(props.detail);
@ -221,7 +237,12 @@ function refresh(withFetch = false) {
return res.json();
})
.then(async (info: SummalyResult & { haveNoteLocally?: boolean } | null) => {
.then(async (info: SummalyResult & {
haveNoteLocally?: boolean,
linkAttribution?: {
userId: string,
}
} | null) => {
unknownUrl.value = info == null;
title.value = info?.title ?? null;
description.value = info?.description ?? null;
@ -236,6 +257,16 @@ function refresh(withFetch = false) {
};
sensitive.value = info?.sensitive ?? false;
activityPub.value = info?.activityPub ?? null;
linkAttribution.value = info?.linkAttribution ?? null;
if (linkAttribution.value) {
try {
const response = await misskeyApi('users/show', { userId: linkAttribution.value.userId });
attributionUser.value = response;
} catch {
// makes the loading ellipsis vanish.
linkAttribution.value = null;
}
}
theNote.value = null;
if (info?.haveNoteLocally) {
@ -395,6 +426,28 @@ refresh();
vertical-align: top;
}
.linkAttributionIcon {
display: inline-block;
width: 16px;
height: 16px;
margin-left: 0.25em;
margin-right: 0.25em;
vertical-align: middle;
border-radius: 50%;
* {
border-radius: 4px;
}
}
.linkAttribution {
width: 100%;
font-size: 0.8em;
display: inline-block;
margin: auto;
padding-top: 0.5em;
text-align: right;
}
.action {
display: flex;
gap: 6px;

View file

@ -15,36 +15,50 @@ SPDX-License-Identifier: AGPL-3.0-only
</template>
<script lang="ts" setup>
import { ref, watch } from 'vue';
import { ref, watch, computed } from 'vue';
import MkTextarea from '@/components/MkTextarea.vue';
import MkInfo from '@/components/MkInfo.vue';
import MkButton from '@/components/MkButton.vue';
import { ensureSignin } from '@/i.js';
import { misskeyApi } from '@/utility/misskey-api.js';
import { i18n } from '@/i18n.js';
import * as os from '@/os.js';
const $i = ensureSignin();
const instanceMutes = ref($i.mutedInstances.join('\n'));
const domainArray = computed(() => {
return instanceMutes.value
.trim().split('\n')
.map(el => el.trim().toLowerCase())
.filter(el => el);
});
const changed = ref(false);
async function save() {
let mutes = instanceMutes.value
.trim().split('\n')
.map(el => el.trim())
.filter(el => el);
// checks for a full line without whitespace.
if (!domainArray.value.every(d => /^\S+$/.test(d))) {
os.alert({
type: 'error',
title: i18n.ts.invalidValue,
});
return;
}
await misskeyApi('i/update', {
mutedInstances: mutes,
mutedInstances: domainArray.value,
});
changed.value = false;
// Refresh filtered list to signal to the user how they've been saved
instanceMutes.value = mutes.join('\n');
instanceMutes.value = domainArray.value.join('\n');
changed.value = false;
}
watch(instanceMutes, () => {
watch(domainArray, (newArray, oldArray) => {
// compare arrays
if (newArray.length !== oldArray.length || !newArray.every((a, i) => a === oldArray[i])) {
changed.value = true;
}
});
</script>

View file

@ -0,0 +1,67 @@
<!--
SPDX-FileCopyrightText: syuilo and misskey-project
SPDX-License-Identifier: AGPL-3.0-only
-->
<template>
<MkTextarea v-model="attributionDomains">
<template #label><SearchLabel>{{ i18n.ts.attributionDomains }}</SearchLabel></template>
<template #caption>
{{ i18n.ts.attributionDomainsDescription }}
<br/>
<Mfm :text="tutorialTag"/>
</template>
</MkTextarea>
<MkButton primary :disabled="!changed" @click="save()"><i class="ti ti-device-floppy"></i> {{ i18n.ts.save }}</MkButton>
</template>
<script lang="ts" setup>
import { ref, watch, computed } from 'vue';
import { host as hostRaw } from '@@/js/config.js';
import { toUnicode } from 'punycode.js';
import MkTextarea from '@/components/MkTextarea.vue';
import MkButton from '@/components/MkButton.vue';
import { ensureSignin } from '@/i.js';
import { misskeyApi } from '@/utility/misskey-api.js';
import { i18n } from '@/i18n.js';
import * as os from '@/os.js';
const $i = ensureSignin();
const attributionDomains = ref($i.attributionDomains.join('\n'));
const domainArray = computed(() => {
return attributionDomains.value
.trim().split('\n')
.map(el => el.trim().toLowerCase())
.filter(el => el);
});
const changed = ref(false);
const tutorialTag = '`<meta name="fediverse:creator" content="' + $i.username + '@' + toUnicode(hostRaw) + '" />`';
async function save() {
// checks for a full line without whitespace.
if (!domainArray.value.every(d => /^\S+$/.test(d))) {
os.alert({
type: 'error',
title: i18n.ts.invalidValue,
});
return;
}
await misskeyApi('i/update', {
attributionDomains: domainArray.value,
});
// Refresh filtered list to signal to the user how they've been saved
attributionDomains.value = domainArray.value.join('\n');
changed.value = false;
}
watch(domainArray, (newArray, oldArray) => {
// compare arrays
if (newArray.length !== oldArray.length || !newArray.every((a, i) => a === oldArray[i])) {
changed.value = true;
}
});
</script>

View file

@ -163,6 +163,13 @@ SPDX-License-Identifier: AGPL-3.0-only
<template #caption>{{ i18n.ts.flagAsBotDescription }}</template>
</MkSwitch>
</SearchMarker>
<SearchMarker
:label="i18n.ts.attributionDomains"
:keywords="['attribution', 'domains', 'preview', 'url']"
>
<AttributionDomainsSettings/>
</SearchMarker>
</div>
</MkFolder>
</SearchMarker>
@ -172,6 +179,7 @@ SPDX-License-Identifier: AGPL-3.0-only
<script lang="ts" setup>
import { computed, reactive, ref, watch, defineAsyncComponent } from 'vue';
import AttributionDomainsSettings from './profile.attribution-domains-setting.vue';
import MkButton from '@/components/MkButton.vue';
import MkInput from '@/components/MkInput.vue';
import MkSwitch from '@/components/MkSwitch.vue';

View file

@ -4292,6 +4292,7 @@ export type components = {
iconUrl: string | null;
displayOrder: number;
})[];
attributionDomains: string[];
};
UserDetailedNotMeOnly: {
/** Format: url */
@ -25185,6 +25186,7 @@ export type operations = {
defaultCWPriority?: 'default' | 'parent' | 'defaultParent' | 'parentDefault';
/** @enum {string} */
allowUnsignedFetch?: 'never' | 'always' | 'essential' | 'staff';
attributionDomains?: string[];
};
};
};

View file

@ -598,6 +598,10 @@ roleAutomatic: "automatic"
translationTimeoutLabel: "Translation timeout"
translationTimeoutCaption: "Timeout in milliseconds for translation API requests."
attributionDomains: "Attribution Domains"
attributionDomainsDescription: "A list of domains whose content can be attributed to you on link previews, separated by new-line. Any subdomain will also be valid. The following needs to be on the webpage:"
writtenBy: "Written by {user}"
followingPub: "Following (Pub)"
followersSub: "Followers (Sub)"
wellKnownResources: "Well-known resources"

View file

@ -7,3 +7,6 @@ openRemoteProfile: "Abrir perfil remoto"
allowClickingNotifications: "Permitir clicar em notificações"
pinnedOnly: "Fixado"
blockingYou: "Bloqueando você"
attributionDomains: "Domínios de Atribuição"
attributionDomainsDescription: "Uma lista de domínios cujo conteúdo pode ser atribuído a você em prévias de link, separadas por linha. Qualquer subdomínio também será válido. O código seguinte precisa estar presente na página:"
writtenBy: "Escrito por {user}"