Files
outline/server/models/Share.ts
Tom Moor 32b76303e5 Add simple count of views to share links (#4036)
* Add simple count of views to share links

* Remove no longer applicable tests

* Avoid incrementing view count for known bots
2022-08-30 23:16:40 -07:00

136 lines
2.4 KiB
TypeScript

import {
ForeignKey,
BelongsTo,
Column,
DefaultScope,
Table,
Scopes,
DataType,
Default,
} from "sequelize-typescript";
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";
@DefaultScope(() => ({
include: [
{
association: "user",
paranoid: false,
},
{
association: "document",
required: false,
},
{
association: "team",
},
],
}))
@Scopes(() => ({
withCollectionPermissions: (userId: string) => {
return {
include: [
{
model: Document.scope("withDrafts"),
paranoid: true,
as: "document",
include: [
{
attributes: [
"id",
"permission",
"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 {
@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;
// getters
get isRevoked() {
return !!this.revokedAt;
}
get canonicalUrl() {
return `${this.team.url}/share/${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(() => Document, "documentId")
document: Document | null;
@ForeignKey(() => Document)
@Column(DataType.UUID)
documentId: string;
revoke(userId: string) {
this.revokedAt = new Date();
this.revokedById = userId;
return this.save();
}
}
export default Share;