From c1e791bad0b079af67398f6407221e6dcbb573dd Mon Sep 17 00:00:00 2001 From: Rigel Kent Date: Wed, 25 Jul 2018 22:01:25 +0200 Subject: expliciting type checks and predicates (server only) --- server/models/activitypub/actor-follow.ts | 8 ++++---- server/models/migrations/index.ts | 23 +++++++++++++++++++++++ server/models/oauth/oauth-token.ts | 9 ++++++--- server/models/video/video-comment.ts | 4 ++-- 4 files changed, 35 insertions(+), 9 deletions(-) create mode 100644 server/models/migrations/index.ts (limited to 'server/models') diff --git a/server/models/activitypub/actor-follow.ts b/server/models/activitypub/actor-follow.ts index b8ce6de1d..adec5e92b 100644 --- a/server/models/activitypub/actor-follow.ts +++ b/server/models/activitypub/actor-follow.ts @@ -111,7 +111,7 @@ export class ActorFollowModel extends Model { if (numberOfActorFollowsRemoved) logger.info('Removed bad %d actor follows.', numberOfActorFollowsRemoved) } - static updateActorFollowsScore (goodInboxes: string[], badInboxes: string[], t: Sequelize.Transaction) { + static updateActorFollowsScore (goodInboxes: string[], badInboxes: string[], t: Sequelize.Transaction | undefined) { if (goodInboxes.length === 0 && badInboxes.length === 0) return logger.info('Updating %d good actor follows and %d bad actor follows scores.', goodInboxes.length, badInboxes.length) @@ -344,7 +344,7 @@ export class ActorFollowModel extends Model { } } - private static incrementScores (inboxUrls: string[], value: number, t: Sequelize.Transaction) { + private static incrementScores (inboxUrls: string[], value: number, t: Sequelize.Transaction | undefined) { const inboxUrlsString = inboxUrls.map(url => `'${url}'`).join(',') const query = `UPDATE "actorFollow" SET "score" = LEAST("score" + ${value}, ${ACTOR_FOLLOW_SCORE.MAX}) ` + @@ -354,10 +354,10 @@ export class ActorFollowModel extends Model { 'WHERE "actor"."inboxUrl" IN (' + inboxUrlsString + ') OR "actor"."sharedInboxUrl" IN (' + inboxUrlsString + ')' + ')' - const options = { + const options = t ? { type: Sequelize.QueryTypes.BULKUPDATE, transaction: t - } + } : undefined return ActorFollowModel.sequelize.query(query, options) } diff --git a/server/models/migrations/index.ts b/server/models/migrations/index.ts new file mode 100644 index 000000000..c2b31b05e --- /dev/null +++ b/server/models/migrations/index.ts @@ -0,0 +1,23 @@ +import * as Sequelize from 'sequelize' + +declare namespace Migration { + interface Boolean extends Sequelize.DefineAttributeColumnOptions { + defaultValue: boolean | null + } + + interface String extends Sequelize.DefineAttributeColumnOptions { + defaultValue: string | null + } + + interface Integer extends Sequelize.DefineAttributeColumnOptions { + defaultValue: number | null + } + + interface BigInteger extends Sequelize.DefineAttributeColumnOptions { + defaultValue: Sequelize.DataTypeBigInt | number | null + } +} + +export { + Migration +} diff --git a/server/models/oauth/oauth-token.ts b/server/models/oauth/oauth-token.ts index 759aa2779..026c30135 100644 --- a/server/models/oauth/oauth-token.ts +++ b/server/models/oauth/oauth-token.ts @@ -154,9 +154,12 @@ export class OAuthTokenModel extends Model { return OAuthTokenModel.scope(ScopeNames.WITH_ACCOUNT) .findOne(query) .then(token => { - token['user'] = token.User - - return token + if (token) { + token['user'] = token.User + return token + } else { + return new OAuthTokenModel() + } }) } diff --git a/server/models/video/video-comment.ts b/server/models/video/video-comment.ts index e79aff209..03122dc03 100644 --- a/server/models/video/video-comment.ts +++ b/server/models/video/video-comment.ts @@ -156,7 +156,7 @@ export class VideoCommentModel extends Model { as: 'InReplyToVideoComment', onDelete: 'CASCADE' }) - InReplyToVideoComment: VideoCommentModel + InReplyToVideoComment: VideoCommentModel | null @ForeignKey(() => VideoModel) @Column @@ -417,7 +417,7 @@ export class VideoCommentModel extends Model { toActivityPubObject (threadParentComments: VideoCommentModel[]): VideoCommentObject { let inReplyTo: string // New thread, so in AS we reply to the video - if (this.inReplyToCommentId === null) { + if ((this.inReplyToCommentId !== null) || (this.InReplyToVideoComment !== null)) { inReplyTo = this.Video.url } else { inReplyTo = this.InReplyToVideoComment.url -- cgit v1.2.3