refresh.ts 2.29 KB
Newer Older
Chocobozzz's avatar
Chocobozzz committed
1
import { logger, loggerTagsFactory } from '@server/helpers/logger'
Chocobozzz's avatar
Chocobozzz committed
2
import { PeerTubeRequestError } from '@server/helpers/requests'
Chocobozzz's avatar
Chocobozzz committed
3
import { ActorLoadByUrlType } from '@server/lib/model-loaders'
Chocobozzz's avatar
Chocobozzz committed
4
5
6
7
8
import { ActorModel } from '@server/models/actor/actor'
import { MActorAccountChannelId, MActorFull } from '@server/types/models'
import { HttpStatusCode } from '@shared/core-utils'
import { fetchRemoteActor } from './shared'
import { APActorUpdater } from './updater'
9
import { getUrlFromWebfinger } from './webfinger'
Chocobozzz's avatar
Chocobozzz committed
10
11
12

async function refreshActorIfNeeded <T extends MActorFull | MActorAccountChannelId> (
  actorArg: T,
Chocobozzz's avatar
Chocobozzz committed
13
  fetchedType: ActorLoadByUrlType
Chocobozzz's avatar
Chocobozzz committed
14
15
16
17
18
19
20
21
): Promise<{ actor: T | MActorFull, refreshed: boolean }> {
  if (!actorArg.isOutdated()) return { actor: actorArg, refreshed: false }

  // We need more attributes
  const actor = fetchedType === 'all'
    ? actorArg as MActorFull
    : await ActorModel.loadByUrlAndPopulateAccountAndChannel(actorArg.url)

Chocobozzz's avatar
Chocobozzz committed
22
23
24
25
  const lTags = loggerTagsFactory('ap', 'actor', 'refresh', actor.url)

  logger.info('Refreshing actor %s.', actor.url, lTags())

Chocobozzz's avatar
Chocobozzz committed
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
  try {
    const actorUrl = await getActorUrl(actor)
    const { actorObject } = await fetchRemoteActor(actorUrl)

    if (actorObject === undefined) {
      logger.warn('Cannot fetch remote actor in refresh actor.')
      return { actor, refreshed: false }
    }

    const updater = new APActorUpdater(actorObject, actor)
    await updater.update()

    return { refreshed: true, actor }
  } catch (err) {
    if ((err as PeerTubeRequestError).statusCode === HttpStatusCode.NOT_FOUND_404) {
Chocobozzz's avatar
Chocobozzz committed
41
      logger.info('Deleting actor %s because there is a 404 in refresh actor.', actor.url, lTags())
Chocobozzz's avatar
Chocobozzz committed
42
43
44
45
46
47
48
49

      actor.Account
        ? await actor.Account.destroy()
        : await actor.VideoChannel.destroy()

      return { actor: undefined, refreshed: false }
    }

Chocobozzz's avatar
Chocobozzz committed
50
    logger.warn('Cannot refresh actor %s.', actor.url, { err, ...lTags() })
Chocobozzz's avatar
Chocobozzz committed
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
    return { actor, refreshed: false }
  }
}

export {
  refreshActorIfNeeded
}

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

function getActorUrl (actor: MActorFull) {
  return getUrlFromWebfinger(actor.preferredUsername + '@' + actor.getHost())
    .catch(err => {
      logger.warn('Cannot get actor URL from webfinger, keeping the old one.', err)
      return actor.url
    })
}