+ const whereActorAnd: WhereOptions[] = [
+ {
+ [Op.or]: [
+ {
+ serverId: null
+ },
+ {
+ serverId: {
+ [Op.in]: Sequelize.literal(inQueryInstanceFollow)
+ }
+ }
+ ]
+ }
+ ]
+
+ let serverRequired = false
+ let whereServer: WhereOptions
+
+ if (options.host && options.host !== WEBSERVER.HOST) {
+ serverRequired = true
+ whereServer = { host: options.host }
+ }
+
+ if (options.host === WEBSERVER.HOST) {
+ whereActorAnd.push({
+ serverId: null
+ })
+ }
+
+ if (Array.isArray(options.handles) && options.handles.length !== 0) {
+ const or: string[] = []
+
+ for (const handle of options.handles || []) {
+ const [ preferredUsername, host ] = handle.split('@')
+
+ if (!host || host === WEBSERVER.HOST) {
+ or.push(`("preferredUsername" = ${VideoChannelModel.sequelize.escape(preferredUsername)} AND "serverId" IS NULL)`)
+ } else {
+ or.push(
+ `(` +
+ `"preferredUsername" = ${VideoChannelModel.sequelize.escape(preferredUsername)} ` +
+ `AND "host" = ${VideoChannelModel.sequelize.escape(host)}` +
+ `)`
+ )
+ }
+ }
+
+ whereActorAnd.push({
+ id: {
+ [Op.in]: literal(`(SELECT "actor".id FROM actor LEFT JOIN server on server.id = actor."serverId" WHERE ${or.join(' OR ')})`)
+ }
+ })
+ }
+
+ const channelActorInclude: Includeable[] = []
+ const accountActorInclude: Includeable[] = []
+
+ if (options.forCount !== true) {
+ accountActorInclude.push({
+ model: ServerModel,
+ required: false
+ })
+
+ accountActorInclude.push({
+ model: ActorImageModel,
+ as: 'Avatars',
+ required: false
+ })
+
+ channelActorInclude.push({
+ model: ActorImageModel,
+ as: 'Avatars',
+ required: false
+ })
+
+ channelActorInclude.push({
+ model: ActorImageModel,
+ as: 'Banners',
+ required: false
+ })
+ }
+
+ if (options.forCount !== true || serverRequired) {
+ channelActorInclude.push({
+ model: ServerModel,
+ duplicating: false,
+ required: serverRequired,
+ where: whereServer
+ })
+ }
+