mirror of
https://github.com/Chocobozzz/PeerTube.git
synced 2025-10-05 10:49:28 +02:00
server/server -> server/core
This commit is contained in:
parent
114327d4ce
commit
5a3d0650c9
838 changed files with 111 additions and 111 deletions
74
server/core/models/server/tracker.ts
Normal file
74
server/core/models/server/tracker.ts
Normal file
|
@ -0,0 +1,74 @@
|
|||
import { AllowNull, BelongsToMany, Column, CreatedAt, Model, Table, UpdatedAt } from 'sequelize-typescript'
|
||||
import { Transaction } from 'sequelize'
|
||||
import { MTracker } from '@server/types/models/server/tracker.js'
|
||||
import { AttributesOnly } from '@peertube/peertube-typescript-utils'
|
||||
import { VideoModel } from '../video/video.js'
|
||||
import { VideoTrackerModel } from './video-tracker.js'
|
||||
|
||||
@Table({
|
||||
tableName: 'tracker',
|
||||
indexes: [
|
||||
{
|
||||
fields: [ 'url' ],
|
||||
unique: true
|
||||
}
|
||||
]
|
||||
})
|
||||
export class TrackerModel extends Model<Partial<AttributesOnly<TrackerModel>>> {
|
||||
|
||||
@AllowNull(false)
|
||||
@Column
|
||||
url: string
|
||||
|
||||
@CreatedAt
|
||||
createdAt: Date
|
||||
|
||||
@UpdatedAt
|
||||
updatedAt: Date
|
||||
|
||||
@BelongsToMany(() => VideoModel, {
|
||||
foreignKey: 'trackerId',
|
||||
through: () => VideoTrackerModel,
|
||||
onDelete: 'CASCADE'
|
||||
})
|
||||
Videos: Awaited<VideoModel>[]
|
||||
|
||||
static listUrlsByVideoId (videoId: number) {
|
||||
const query = {
|
||||
include: [
|
||||
{
|
||||
attributes: [ 'id' ],
|
||||
model: VideoModel.unscoped(),
|
||||
required: true,
|
||||
where: { id: videoId }
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
return TrackerModel.findAll(query)
|
||||
.then(rows => rows.map(rows => rows.url))
|
||||
}
|
||||
|
||||
static findOrCreateTrackers (trackers: string[], transaction: Transaction): Promise<MTracker[]> {
|
||||
if (trackers === null) return Promise.resolve([])
|
||||
|
||||
const tasks: Promise<MTracker>[] = []
|
||||
trackers.forEach(tracker => {
|
||||
const query = {
|
||||
where: {
|
||||
url: tracker
|
||||
},
|
||||
defaults: {
|
||||
url: tracker
|
||||
},
|
||||
transaction
|
||||
}
|
||||
|
||||
const promise = TrackerModel.findOrCreate<MTracker>(query)
|
||||
.then(([ trackerInstance ]) => trackerInstance)
|
||||
tasks.push(promise)
|
||||
})
|
||||
|
||||
return Promise.all(tasks)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue