<commit_before>import { Transaction } from 'sequelize'
import { LocalVideoViewerModel } from '@server/models/view/local-video-viewer'
import { LocalVideoViewerWatchSectionModel } from '@server/models/view/local-video-viewer-watch-section'
import { MVideo } from '@server/types/models'
import { WatchActionObject } from '@shared/models'
import { getDurationFromActivityStream } from './activity'

async function createOrUpdateLocalVideoViewer (watchAction: WatchActionObject, video: MVideo, t: Transaction) {
  const stats = await LocalVideoViewerModel.loadByUrl(watchAction.id)
  if (stats) await stats.destroy({ transaction: t })

  const localVideoViewer = await LocalVideoViewerModel.create({
    url: watchAction.id,
    uuid: watchAction.uuid,

    watchTime: getDurationFromActivityStream(watchAction.duration),

    startDate: new Date(watchAction.startTime),
    endDate: new Date(watchAction.endTime),

    country: watchAction.location
      ? watchAction.location.addressCountry
      : null,

    videoId: video.id
  })

  await LocalVideoViewerWatchSectionModel.bulkCreateSections({
    localVideoViewerId: localVideoViewer.id,

    watchSections: watchAction.watchSections.map(s => ({
      start: s.startTimestamp,
      end: s.endTimestamp
    }))
  })
}

// ---------------------------------------------------------------------------

export {
  createOrUpdateLocalVideoViewer
}
<commit_msg>Fix transaction when processing local viewer<commit_after>import { Transaction } from 'sequelize'
import { LocalVideoViewerModel } from '@server/models/view/local-video-viewer'
import { LocalVideoViewerWatchSectionModel } from '@server/models/view/local-video-viewer-watch-section'
import { MVideo } from '@server/types/models'
import { WatchActionObject } from '@shared/models'
import { getDurationFromActivityStream } from './activity'

async function createOrUpdateLocalVideoViewer (watchAction: WatchActionObject, video: MVideo, t: Transaction) {
  const stats = await LocalVideoViewerModel.loadByUrl(watchAction.id)
  if (stats) await stats.destroy({ transaction: t })

  const localVideoViewer = await LocalVideoViewerModel.create({
    url: watchAction.id,
    uuid: watchAction.uuid,

    watchTime: getDurationFromActivityStream(watchAction.duration),

    startDate: new Date(watchAction.startTime),
    endDate: new Date(watchAction.endTime),

    country: watchAction.location
      ? watchAction.location.addressCountry
      : null,

    videoId: video.id
  }, { transaction: t })

  await LocalVideoViewerWatchSectionModel.bulkCreateSections({
    localVideoViewerId: localVideoViewer.id,

    watchSections: watchAction.watchSections.map(s => ({
      start: s.startTimestamp,
      end: s.endTimestamp
    })),

    transaction: t
  })
}

// ---------------------------------------------------------------------------

export {
  createOrUpdateLocalVideoViewer
}

{'fraction_non_alphanumeric': 0.14681440443213298, 'fraction_numerical': 0.0, 'mean_word_length': 3.5930047694753577, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2640939', 'n_tokens_mistral': 804, 'n_tokens_neox': 788, 'n_words': 163}