mirror of
https://github.com/outline/outline.git
synced 2026-11-25 23:24:06 +03:00
poc
This commit is contained in:
1 parent
df3dde951a
commit
08efb34c29
6 files changed
+78
-42
No files matched your search
@@ -7,6 +7,7 @@ import { MenuItem } from "@shared/editor/types";
|
||||
import { MentionType } from "@shared/types";
|
||||
import parseDocumentSlug from "@shared/utils/parseDocumentSlug";
|
||||
import User from "~/models/User";
|
||||
import Model from "~/models/base/Model";
|
||||
import Avatar from "~/components/Avatar";
|
||||
import { AvatarSize } from "~/components/Avatar/Avatar";
|
||||
import Flex from "~/components/Flex";
|
||||
@@ -19,7 +20,7 @@ import SuggestionsMenu, {
|
||||
|
||||
interface MentionItem extends MenuItem {
|
||||
name: string;
|
||||
user: User;
|
||||
model: Model;
|
||||
appendSpace: boolean;
|
||||
attrs: {
|
||||
id: string;
|
||||
@@ -39,7 +40,7 @@ function MentionMenu({ search, isActive, ...rest }: Props) {
|
||||
const [loaded, setLoaded] = React.useState(false);
|
||||
const [items, setItems] = React.useState<MentionItem[]>([]);
|
||||
const { t } = useTranslation();
|
||||
const { auth, users } = useStores();
|
||||
const { auth, documents, users } = useStores();
|
||||
const location = useLocation();
|
||||
const documentId = parseDocumentSlug(location.pathname);
|
||||
const { data, loading, request } = useRequest(
|
||||
@@ -60,19 +61,35 @@ function MentionMenu({ search, isActive, ...rest }: Props) {
|
||||
|
||||
React.useEffect(() => {
|
||||
if (data && !loading) {
|
||||
const items = data.map((user) => ({
|
||||
name: "mention",
|
||||
user,
|
||||
title: user.name,
|
||||
appendSpace: true,
|
||||
attrs: {
|
||||
id: v4(),
|
||||
type: MentionType.User,
|
||||
modelId: user.id,
|
||||
actorId: auth.currentUserId ?? undefined,
|
||||
label: user.name,
|
||||
},
|
||||
}));
|
||||
const items = data
|
||||
.map((user) => ({
|
||||
name: "mention",
|
||||
model: user,
|
||||
title: user.name,
|
||||
appendSpace: true,
|
||||
attrs: {
|
||||
id: v4(),
|
||||
type: MentionType.User,
|
||||
modelId: user.id,
|
||||
actorId: auth.currentUserId ?? undefined,
|
||||
label: user.name,
|
||||
},
|
||||
}))
|
||||
.concat(
|
||||
documents.orderedData.map((doc) => ({
|
||||
name: "mention",
|
||||
model: doc,
|
||||
title: doc.title,
|
||||
appendSpace: true,
|
||||
attrs: {
|
||||
id: v4(),
|
||||
type: MentionType.Document,
|
||||
modelId: doc.id,
|
||||
actorId: auth.currentUserId ?? undefined,
|
||||
label: doc.title,
|
||||
},
|
||||
}))
|
||||
);
|
||||
|
||||
setItems(items);
|
||||
setLoaded(true);
|
||||
@@ -105,7 +122,7 @@ function MentionMenu({ search, isActive, ...rest }: Props) {
|
||||
style={{ width: 24, height: 24 }}
|
||||
>
|
||||
<Avatar
|
||||
model={item.user}
|
||||
model={item.model as User}
|
||||
showBorder={false}
|
||||
alt={t("Profile picture")}
|
||||
size={AvatarSize.Small}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import dns from "dns";
|
||||
import Router from "koa-router";
|
||||
import { UnfurlResourceType } from "@shared/types";
|
||||
import { MentionType, UnfurlResourceType } from "@shared/types";
|
||||
import { getBaseDomain, parseDomain } from "@shared/utils/domains";
|
||||
import parseDocumentSlug from "@shared/utils/parseDocumentSlug";
|
||||
import parseMentionUrl from "@shared/utils/parseMentionUrl";
|
||||
@@ -36,28 +36,32 @@ router.post(
|
||||
if (!documentId) {
|
||||
throw ValidationError("Document ID is required to unfurl a mention");
|
||||
}
|
||||
const { modelId: userId } = parseMentionUrl(url);
|
||||
const { modelId, mentionType } = parseMentionUrl(url);
|
||||
|
||||
const [user, document] = await Promise.all([
|
||||
User.findByPk(userId),
|
||||
Document.findByPk(documentId, {
|
||||
userId: actor.id,
|
||||
}),
|
||||
]);
|
||||
if (!user) {
|
||||
throw NotFoundError("Mentioned user does not exist");
|
||||
}
|
||||
if (!document) {
|
||||
throw NotFoundError("Document does not exist");
|
||||
}
|
||||
authorize(actor, "read", user);
|
||||
authorize(actor, "read", document);
|
||||
// TODO: Add support for other mention types
|
||||
if (mentionType === MentionType.User) {
|
||||
const [user, document] = await Promise.all([
|
||||
User.findByPk(modelId),
|
||||
Document.findByPk(documentId, {
|
||||
userId: actor.id,
|
||||
}),
|
||||
]);
|
||||
if (!user) {
|
||||
throw NotFoundError("Mentioned user does not exist");
|
||||
}
|
||||
if (!document) {
|
||||
throw NotFoundError("Document does not exist");
|
||||
}
|
||||
authorize(actor, "read", user);
|
||||
authorize(actor, "read", document);
|
||||
|
||||
ctx.body = await presentUnfurl({
|
||||
type: UnfurlResourceType.Mention,
|
||||
user,
|
||||
document,
|
||||
});
|
||||
}
|
||||
|
||||
ctx.body = await presentUnfurl({
|
||||
type: UnfurlResourceType.Mention,
|
||||
user,
|
||||
document,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ import { Primitive } from "utility-types";
|
||||
import validator from "validator";
|
||||
import isIn from "validator/lib/isIn";
|
||||
import isUUID from "validator/lib/isUUID";
|
||||
import { CollectionPermission } from "@shared/types";
|
||||
import { CollectionPermission, MentionType } from "@shared/types";
|
||||
import { UrlHelper } from "@shared/utils/UrlHelper";
|
||||
import { validateColorHex } from "@shared/utils/color";
|
||||
import { validateIndexCharacters } from "@shared/utils/indexCharacters";
|
||||
@@ -233,7 +233,12 @@ export class ValidateURL {
|
||||
}
|
||||
|
||||
const { id, mentionType, modelId } = parseMentionUrl(url);
|
||||
return id && isUUID(id) && mentionType === "user" && isUUID(modelId);
|
||||
return (
|
||||
id &&
|
||||
isUUID(id) &&
|
||||
Object.values(MentionType).includes(mentionType as MentionType) &&
|
||||
isUUID(modelId)
|
||||
);
|
||||
} catch (err) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -294,9 +294,13 @@ width: 100%;
|
||||
font-size: 0.9em;
|
||||
cursor: default;
|
||||
|
||||
&:before {
|
||||
&.mention-user:before {
|
||||
content: "@";
|
||||
}
|
||||
|
||||
&.mention-document:before {
|
||||
content: "+";
|
||||
}
|
||||
}
|
||||
|
||||
> div {
|
||||
|
||||
@@ -7,6 +7,7 @@ import {
|
||||
} from "prosemirror-model";
|
||||
import { Command, TextSelection } from "prosemirror-state";
|
||||
import { Primitive } from "utility-types";
|
||||
import { MentionType } from "@shared/types";
|
||||
import Extension from "../lib/Extension";
|
||||
import { MarkdownSerializerState } from "../lib/markdown/serializer";
|
||||
import mentionRule from "../rules/mention";
|
||||
@@ -50,10 +51,14 @@ export default class Mention extends Extension {
|
||||
},
|
||||
],
|
||||
toDOM: (node) => [
|
||||
"span",
|
||||
"a",
|
||||
{
|
||||
class: `${node.type.name} use-hover-preview`,
|
||||
class: `${node.type.name} mention-${node.attrs.type} use-hover-preview`,
|
||||
id: node.attrs.id,
|
||||
href:
|
||||
node.attrs.type === MentionType.User
|
||||
? undefined
|
||||
: `/doc/${node.attrs.modelId}`,
|
||||
"data-type": node.attrs.type,
|
||||
"data-id": node.attrs.modelId,
|
||||
"data-actorId": node.attrs.actorId,
|
||||
|
||||
@@ -47,6 +47,7 @@ export enum FileOperationState {
|
||||
|
||||
export enum MentionType {
|
||||
User = "user",
|
||||
Document = "document",
|
||||
}
|
||||
|
||||
export type PublicEnv = {
|
||||
|
||||
Reference in new issue
Block a user