Files
outline/server/models/Share.ts
Hemachandar d3eb3db7ba feat: Public sharing of collections (#9529)
* shares.info, collections.info, documents.info

* shares.list, shares.create, shares.update

* shares.sitemap

* parity with existing document shared screen

* collection share popover

* parent share and table

* collection scene

* collection link in sidebar

* sidebar and breadcrumb collection link click

* collection link click in editor

* meta

* more meta + 404 page

* map internal link, remove showLastUpdated option

* fix shares.list pagination

* show last updated

* shareLoader tests

* lint

* sidebar context for collection link

* badge in shares table

* fix existing tests

* tsc

* update failing test snapshot

* env

* signed url for collection attachments

* include collection content in SSR for screen readers

* search

* drafts can be shared

* review

* tsc, remove old shared-doc scene

* tweaks

* DRY

* refactor loader

* Remove share/collection urls

* fix: Collection overview should not be editable when viewing shared link and logged in

* Tweak public breadcrumb

* fix: Deleted documents should never be exposed through share

* empty sharedTree array where includeChildDocuments is false

* revert includeChildDocs guard for logical correctness + SSR bug fix

* fix: check document is part of share

---------

Co-authored-by: Tom Moor <tom@getoutline.com>
2025-08-03 13:07:39 -04:00

239 lines
4.6 KiB
TypeScript

import {
InferAttributes,
InferCreationAttributes,
type SaveOptions,
} from "sequelize";
import {
ForeignKey,
BelongsTo,
Column,
DefaultScope,
Table,
Scopes,
DataType,
Default,
AllowNull,
Is,
Unique,
BeforeUpdate,
} from "sequelize-typescript";
import { UrlHelper } from "@shared/utils/UrlHelper";
import env from "@server/env";
import { ValidationError } from "@server/errors";
import { APIContext } from "@server/types";
import Collection from "./Collection";
import Document from "./Document";
import Team from "./Team";
import User from "./User";
import IdModel from "./base/IdModel";
import Fix from "./decorators/Fix";
import IsFQDN from "./validators/IsFQDN";
import Length from "./validators/Length";
@DefaultScope(() => ({
include: [
{
association: "user",
paranoid: false,
},
{
association: "collection",
required: false,
},
{
association: "document",
required: false,
},
{
association: "team",
},
],
}))
@Scopes(() => ({
withCollectionPermissions: (userId: string) => ({
include: [
{
attributes: [
"id",
"name",
"permission",
"sharing",
"urlId",
"teamId",
"deletedAt",
],
model: Collection.scope({
method: ["withMembership", userId],
}),
as: "collection",
},
{
model: Document.scope([
"withDrafts",
{
method: ["withMembership", userId],
},
]),
paranoid: true,
as: "document",
include: [
{
attributes: [
"id",
"name",
"permission",
"urlId",
"sharing",
"teamId",
"deletedAt",
],
model: Collection.scope({
method: ["withMembership", userId],
}),
as: "collection",
},
],
},
{
association: "user",
paranoid: false,
},
{
association: "team",
},
],
}),
}))
@Table({ tableName: "shares", modelName: "share" })
@Fix
class Share extends IdModel<
InferAttributes<Share>,
Partial<InferCreationAttributes<Share>>
> {
@Column
published: boolean;
@Column
includeChildDocuments: boolean;
@Column
revokedAt: Date | null;
@Column
lastAccessedAt: Date | null;
/** Total count of times the shared link has been accessed */
@Default(0)
@Column
views: number;
@AllowNull
@Is({
args: UrlHelper.SHARE_URL_SLUG_REGEX,
msg: "Must be only alphanumeric and dashes",
})
@Column
urlId: string | null | undefined;
@Unique
@Length({ max: 255, msg: "domain must be 255 characters or less" })
@IsFQDN
@Column
domain: string | null;
@Default(false)
@Column
allowIndexing: boolean;
@Default(false)
@Column
showLastUpdated: boolean;
// hooks
@BeforeUpdate
static async checkDomain(model: Share, options: SaveOptions) {
if (!model.domain) {
return model;
}
model.domain = model.domain.toLowerCase();
const count = await Team.count({
...options,
where: {
domain: model.domain,
},
});
if (count > 0) {
throw ValidationError("Domain is already in use");
}
return model;
}
// getters
get isRevoked() {
return !!this.revokedAt;
}
get canonicalUrl() {
if (this.domain) {
const url = new URL(env.URL);
return `${url.protocol}//${this.domain}${url.port ? `:${url.port}` : ""}`;
}
return this.urlId
? `${this.team.url}/s/${this.urlId}`
: `${this.team.url}/s/${this.id}`;
}
// associations
@BelongsTo(() => User, "revokedById")
revokedBy: User;
@ForeignKey(() => User)
@Column(DataType.UUID)
revokedById: string;
@BelongsTo(() => User, "userId")
user: User;
@ForeignKey(() => User)
@Column(DataType.UUID)
userId: string;
@BelongsTo(() => Team, "teamId")
team: Team;
@ForeignKey(() => Team)
@Column(DataType.UUID)
teamId: string;
@BelongsTo(() => Collection, "collectionId")
collection: Collection | null;
@ForeignKey(() => Collection)
@Column(DataType.UUID)
collectionId: string | null;
@BelongsTo(() => Document, "documentId")
document: Document | null;
@ForeignKey(() => Document)
@Column(DataType.UUID)
documentId: string | null;
revoke(ctx: APIContext) {
const { user } = ctx.state.auth;
this.revokedAt = new Date();
this.revokedById = user.id;
return this.saveWithCtx(ctx, undefined, { name: "revoke" });
}
}
export default Share;