Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/entities/game-channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default class GameChannel {
@Property()
private: boolean = false

@ManyToOne(() => Game)
@ManyToOne(() => Game, { eager: true })
game: Game

@Required({
Expand All @@ -66,6 +66,12 @@ export default class GameChannel {
@Property({ onUpdate: () => new Date() })
updatedAt: Date = new Date()

static getSearchCacheKey(game: Game, wildcard = false) {
let key = `channels-search-${game.id}`
if (wildcard) key += '-*'
return key
}

constructor(game: Game) {
this.game = game
}
Expand Down
112 changes: 60 additions & 52 deletions src/services/game-channel.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import buildErrorResponse from '../lib/errors/buildErrorResponse'
import { captureException } from '@sentry/node'
import { pageValidation } from '../lib/pagination/pageValidation'
import { DEFAULT_PAGE_SIZE } from '../lib/pagination/itemsPerPage'
import { withResponseCache } from '../lib/perf/responseCache'
import Game from '../entities/game'

const itemsPerPage = DEFAULT_PAGE_SIZE
Expand All @@ -30,69 +31,76 @@ export default class GameChannelService extends Service {
const em: EntityManager = req.ctx.em

const game: Game = req.ctx.state.game

const query = em.qb(GameChannel, 'gc')
.select('gc.*')
.orderBy({ totalMessages: QueryOrder.DESC })
.limit(itemsPerPage + 1)
.offset(Number(page) * itemsPerPage)

if (search) {
query.andWhere({
$or: [
{ name: { $like: `%${search}%` } },
{
owner: { identifier: { $like: `%${search}%` } }
}
]
})
}

if (req.ctx.state.user.api) {
query.andWhere({
private: false
})
}

if (propKey) {
if (propValue) {
const searchComponent = search ? encodeURIComponent(search) : 'no-search'
const cacheKey = `${GameChannel.getSearchCacheKey(game)}-${searchComponent}-${page}-${propKey}-${propValue}`

return withResponseCache({
key: cacheKey,
ttl: 600
}, async () => {
const query = em.qb(GameChannel, 'gc')
.select('gc.*')
.orderBy({ totalMessages: QueryOrder.DESC })
.limit(itemsPerPage + 1)
.offset(Number(page) * itemsPerPage)

if (search) {
query.andWhere({
props: {
$some: {
key: propKey,
value: propValue
$or: [
{ name: { $like: `%${search}%` } },
{
owner: { identifier: { $like: `%${search}%` } }
}
}
]
})
} else {
}

if (req.ctx.state.user.api) {
query.andWhere({
props: {
$some: {
key: propKey
}
}
private: false
})
}
}

const [channels, count] = await query
.andWhere({ game })
.getResultAndCount()
if (propKey) {
if (propValue) {
query.andWhere({
props: {
$some: {
key: propKey,
value: propValue
}
}
})
} else {
query.andWhere({
props: {
$some: {
key: propKey
}
}
})
}
}

await em.populate(channels, ['owner'])
const [channels, count] = await query
.andWhere({ game })
.getResultAndCount()

const channelPromises = channels.slice(0, itemsPerPage)
.map((channel) => channel.toJSONWithCount(req.ctx.state.includeDevData))
await em.populate(channels, ['owner'])

return {
status: 200,
body: {
channels: await Promise.all(channelPromises),
count,
itemsPerPage,
isLastPage: channels.length <= itemsPerPage
const channelPromises = channels.slice(0, itemsPerPage)
.map((channel) => channel.toJSONWithCount(req.ctx.state.includeDevData))

return {
status: 200,
body: {
channels: await Promise.all(channelPromises),
count,
itemsPerPage,
isLastPage: channels.length <= itemsPerPage
}
}
}
})
}

@Route({
Expand Down
33 changes: 33 additions & 0 deletions src/subscribers/game-channel.subscriber.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { EventArgs, EventSubscriber } from '@mikro-orm/mysql'
import { deferClearResponseCache } from '../lib/perf/responseCacheQueue'
import GameChannel from '../entities/game-channel'
import GameChannelProp from '../entities/game-channel-prop'

export class GameChannelSubscriber implements EventSubscriber {
getSubscribedEntities() {
return [GameChannel, GameChannelProp]
}

async clearSearchCacheKey(entity: GameChannel | GameChannelProp) {
const channel = entity instanceof GameChannel ? entity : entity.gameChannel

if (!channel) {
// can happen when a prop is being deleted, the reference to the channel is gone
return
}

await deferClearResponseCache(GameChannel.getSearchCacheKey(channel.game, true))
}

afterCreate(args: EventArgs<GameChannel | GameChannelProp>) {
void this.clearSearchCacheKey(args.entity)
}

afterUpdate(args: EventArgs<GameChannel | GameChannelProp>): void | Promise<void> {
void this.clearSearchCacheKey(args.entity)
}

afterDelete(args: EventArgs<GameChannel | GameChannelProp>) {
void this.clearSearchCacheKey(args.entity)
}
}
2 changes: 2 additions & 0 deletions src/subscribers/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { GameChannelSubscriber } from './game-channel.subscriber'
import { LeaderboardEntrySubscriber } from './leaderboard-entry.subscriber'
import { PlayerGameStatSubscriber } from './player-game-stat.subscriber'

export const subscribers = [
GameChannelSubscriber,
LeaderboardEntrySubscriber,
PlayerGameStatSubscriber
]
Loading