Skip to content

Commit

Permalink
fix(network): fix connection cache and debug mode
Browse files Browse the repository at this point in the history
  • Loading branch information
dev-737 committed May 5, 2024
1 parent 977c8db commit 3169583
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ jobs:
docker cp $(docker ps -alq):/src/logs .
cp -r logs/ ~/console-logs/interchat-logs-$(date +"%Y-%m-%d_%H-%M-%S")
- name: Remove old containers and images
run: sudo docker rm $(docker ps -a -q) && sudo docker rmi $(docker images -q)
run: |
docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)
docker rmi $(docker images -q)
- name: Run latest image
run: sudo docker run -d --env-file ~/important/interchat-env ghcr.io/discord-interchat/interchat
run: docker run -d --env-file ~/important/interchat-env ghcr.io/discord-interchat/interchat
25 changes: 12 additions & 13 deletions src/managers/NetworkManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const MAX_STORE = 3;
export default class NetworkManager {
private readonly scheduler: Scheduler;
private readonly antiSpamMap: Collection<string, AntiSpamUserOpts>;
private _connectionCache: Collection<string, Connection>;
private _connectionCache: Collection<string, connectedList>;
private cachePopulated = false;

constructor() {
Expand All @@ -61,10 +61,7 @@ export default class NetworkManager {

protected async populateConnectionCache() {
Logger.debug('[InterChat]: Populating connection cache.');
const connections = await db.connectedList.findMany({
where: { connected: true },
include: { hub: true },
});
const connections = await db.connectedList.findMany({ where: { connected: true } });

// populate all at once without time delay
this._connectionCache = new Collection(connections.map((c) => [c.channelId, c]));
Expand All @@ -91,12 +88,15 @@ export default class NetworkManager {
const locale = await message.client.getUserLocale(message.author.id);
message.author.locale = locale;

const connection = this._connectionCache.get(message.channelId);

// check if the message was sent in a network channel
if (!connection?.connected || !connection.hub) return;
const connection = this.connectionCache.get(message.channel.id);
if (!connection?.connected) return;

const hub = await db.hubs.findFirst({ where: { id: connection?.hubId } });
if (!hub) return;

const settings = new HubSettingsBitField(connection.hub.settings);
const settings = new HubSettingsBitField(hub.settings);
const hubConnections = this.connectionCache.filter((con) => con.hubId === connection.hubId);

const attachment = message.attachments.first();
const attachmentURL = attachment
Expand All @@ -108,7 +108,6 @@ export default class NetworkManager {
return;
}

const hubConnections = this._connectionCache.filter((con) => con.hubId === connection.hubId);

const censoredContent = censor(message.content);

Expand Down Expand Up @@ -189,8 +188,8 @@ export default class NetworkManager {
let messageFormat: WebhookMessageCreateOptions = {
components: jumpButton ? [jumpButton] : undefined,
embeds: [otherConnection.profFilter ? censoredEmbed : embed],
username: `${connection.hub?.name}`,
avatarURL: connection.hub?.iconUrl,
username: `${hub.name}`,
avatarURL: hub.iconUrl,
threadId: otherConnection.parentId ? otherConnection.channelId : undefined,
allowedMentions: { parse: [] },
};
Expand Down Expand Up @@ -284,7 +283,7 @@ export default class NetworkManager {
{ phrase: 'network.welcome', locale },
{
user: message.author.toString(),
hub: connection.hub.name,
hub: hub.name,
channel: message.channel.toString(),
totalServers: hubConnections.size.toString(),
emoji: emojis.wave_anim,
Expand Down
2 changes: 1 addition & 1 deletion src/utils/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default createLogger({
transports: [
new transports.Console({
format: format.combine(format.colorize(), custom),
level: isDevBuild ? 'debug' : 'info',
level: isDevBuild || process.env.DEBUG ? 'debug' : 'info',
}),
new transports.File({ filename: 'logs/error.log', level: 'error' }),
new transports.File({
Expand Down

0 comments on commit 3169583

Please sign in to comment.