-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
61d951d
commit b435ca2
Showing
7 changed files
with
60 additions
and
51 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,52 @@ | ||
const emojis = require("./emojis"); | ||
const { PrismaClient } = require("@prisma/client"); | ||
const prisma = new PrismaClient(); | ||
module.exports = async function generateFullTimeline(messages) { | ||
const intervalMessages = {}; | ||
messages.forEach((message) => { | ||
const timestamp = parseFloat(message.ts); | ||
const interval = Math.floor(timestamp / 10) * 10; | ||
|
||
if (!intervalMessages[interval]) { | ||
intervalMessages[interval] = new Set(); | ||
|
||
|
||
|
||
|
||
async function getChannelEmoji(channelId) { | ||
const channel = await prisma.channel.findFirst({ | ||
where: { | ||
id: channelId | ||
} | ||
}) | ||
if (!channel || !channel.emoji) return "💥" | ||
return channel.emoji | ||
} | ||
|
||
|
||
async function generateMessageString(messages, currentTime) { | ||
const interval = 10; | ||
const secondsInDay = 86400; | ||
const intervalsInDay = secondsInDay / interval; | ||
|
||
let messageString = ''; | ||
const timeToEmojiMap = {}; | ||
|
||
for (const message of messages) { | ||
const messageTime = parseInt(message.ts.split('.')[0], 10); | ||
const timeDiff = currentTime - messageTime; | ||
const intervalIndex = Math.floor(timeDiff / interval); | ||
|
||
if (intervalIndex < intervalsInDay) { | ||
const emoji = await getChannelEmoji(message.channel.id); | ||
timeToEmojiMap[intervalIndex] = { emoji, permalink: message.permalink }; | ||
} | ||
} | ||
|
||
intervalMessages[interval].add(message.channel.id); | ||
}); | ||
|
||
const startTime = Math.min(...Object.keys(intervalMessages).map(Number)); | ||
const endTime = Math.max(...Object.keys(intervalMessages).map(Number)); | ||
let output = ""; | ||
await prisma.$connect(); | ||
|
||
for (let time = startTime; time <= endTime; time += 10) { | ||
if (intervalMessages[time]) { | ||
for (const channelId of intervalMessages[time]) { | ||
var emoji = "💥,"; | ||
const channelRecord = await prisma.channel.findFirst({ | ||
where: { | ||
id: channelId, | ||
}, | ||
}); | ||
if (!channelRecord || !channelRecord.emoji) { | ||
output += `<https://hackclub.slack.com/archives/${channelId}|${emoji}>`; | ||
continue; | ||
} | ||
output += `<https://hackclub.slack.com/archives/${channelId}|${channelRecord.emoji}>,`; | ||
} | ||
for (let i = 0; i < intervalsInDay; i++) { | ||
if (timeToEmojiMap[i]) { | ||
const { emoji, permalink } = timeToEmojiMap[i]; | ||
messageString += `<${permalink}|${emoji}>,`; | ||
} else { | ||
output += "-,"; | ||
messageString += '-,'; | ||
} | ||
} | ||
|
||
let chars = output.split(","); | ||
chars = chars | ||
.map((char) => (emojis.includes(char) || char == "-" ? char : "")) | ||
.slice(chars.length - 40, chars.length); | ||
await prisma.$disconnect(); | ||
return chars | ||
.join(",") | ||
.replaceAll(",", "") | ||
.replaceAll("@", "@") | ||
.replaceAll(/[\u{1F3FB}-\u{1F3FF}]/gmu, ""); | ||
}; | ||
return messageString.split(",").slice(0, 10).join(",").replaceAll(",", ""); | ||
} | ||
|
||
|
||
module.exports = generateMessageString | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters