feat: Pin to home (#2880)

This commit is contained in:
Tom Moor
2021-12-30 16:54:02 -08:00
committed by GitHub
parent 5be2eb75f3
commit eb0c324da8
57 changed files with 1884 additions and 819 deletions

View File

@@ -142,6 +142,7 @@ Document.associate = (models) => {
as: "updatedBy",
foreignKey: "lastModifiedById",
});
/** Deprecated use Pins relationship instead */
Document.belongsTo(models.User, {
as: "pinnedBy",
foreignKey: "pinnedById",
@@ -180,8 +181,7 @@ Document.associate = (models) => {
},
},
});
// @ts-expect-error ts-migrate(7006) FIXME: Parameter 'userId' implicitly has an 'any' type.
Document.addScope("withCollection", (userId, paranoid = true) => {
Document.addScope("withCollection", (userId: string, paranoid = true) => {
if (userId) {
return {
include: [
@@ -219,8 +219,7 @@ Document.associate = (models) => {
},
],
});
// @ts-expect-error ts-migrate(7006) FIXME: Parameter 'userId' implicitly has an 'any' type.
Document.addScope("withViews", (userId) => {
Document.addScope("withViews", (userId: string) => {
if (!userId) return {};
return {
include: [
@@ -236,8 +235,7 @@ Document.associate = (models) => {
],
};
});
// @ts-expect-error ts-migrate(7006) FIXME: Parameter 'userId' implicitly has an 'any' type.
Document.addScope("withStarred", (userId) => ({
Document.addScope("withStarred", (userId: string) => ({
include: [
{
model: models.Star,
@@ -250,20 +248,40 @@ Document.associate = (models) => {
},
],
}));
Document.defaultScopeWithUser = (userId: string) => {
const starredScope = {
method: ["withStarred", userId],
};
const collectionScope = {
method: ["withCollection", userId],
};
const viewScope = {
method: ["withViews", userId],
};
return Document.scope(
"defaultScope",
starredScope,
collectionScope,
viewScope
);
};
};
// @ts-expect-error ts-migrate(7006) FIXME: Parameter 'id' implicitly has an 'any' type.
Document.findByPk = async function (id, options = {}) {
Document.findByPk = async function (
id: string,
options: {
userId?: string;
paranoid?: boolean;
} = {}
) {
// allow default preloading of collection membership if `userId` is passed in find options
// almost every endpoint needs the collection membership to determine policy permissions.
const scope = this.scope(
"withUnpublished",
{
// @ts-expect-error ts-migrate(2339) FIXME: Property 'userId' does not exist on type '{}'.
method: ["withCollection", options.userId, options.paranoid],
},
{
// @ts-expect-error ts-migrate(2339) FIXME: Property 'userId' does not exist on type '{}'.
method: ["withViews", options.userId],
}
);
@@ -275,10 +293,13 @@ Document.findByPk = async function (id, options = {}) {
},
...options,
});
} else if (id.match(SLUG_URL_REGEX)) {
}
const match = id.match(SLUG_URL_REGEX);
if (match) {
return scope.findOne({
where: {
urlId: id.match(SLUG_URL_REGEX)[1],
urlId: match[1],
},
...options,
});