(
channel: { id: string; channelName: string },
linenBot: users,
displayName: string
)
| 29 | } |
| 30 | |
| 31 | async function notifyChannel( |
| 32 | channel: { id: string; channelName: string }, |
| 33 | linenBot: users, |
| 34 | displayName: string |
| 35 | ) { |
| 36 | const lastThread = await prisma.threads.findFirst({ |
| 37 | select: { messages: true, id: true }, |
| 38 | where: { channelId: channel.id }, |
| 39 | orderBy: { sentAt: 'desc' }, |
| 40 | }); |
| 41 | // if last message from channel is from linen-bot, lets append the welcome for the new user |
| 42 | if ( |
| 43 | lastThread && |
| 44 | lastThread.messages.length && |
| 45 | lastThread.messages[0].usersId === linenBot.id && |
| 46 | lastThread.messages[0].body.startsWith('Welcome') |
| 47 | ) { |
| 48 | let body = processMessage(lastThread.messages[0].body, displayName); |
| 49 | await prisma.messages.update({ |
| 50 | data: { |
| 51 | body, |
| 52 | }, |
| 53 | where: { id: lastThread.messages[0].id }, |
| 54 | }); |
| 55 | } else { |
| 56 | // new thread will be created |
| 57 | await createThread({ |
| 58 | body: `Welcome! \`${displayName}\` joined #${channel.channelName}.`, |
| 59 | channelId: channel.id, |
| 60 | linenBotId: linenBot.id, |
| 61 | }); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | export function processMessage(body: string, displayName: string) { |
| 66 | let split = body.split('`'); |
no test coverage detected