Skip to content

Commit

Permalink
Code cleaning, fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ojaha065 committed Oct 14, 2023
1 parent 3cb4694 commit fb53ea0
Show file tree
Hide file tree
Showing 20 changed files with 187 additions and 103 deletions.
4 changes: 3 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
"plugin:import/recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"plugin:import/typescript"
"plugin:import/typescript",
"alloy",
"alloy/typescript"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
Expand Down
97 changes: 76 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "lounasbotti",
"type": "module",
"version": "1.8.0",
"version": "1.8.1",
"private": true,
"description": "Slack bot for retrieving lunch menus of local restaurants. Very much WIP and not meant for public use.",
"main": "./dist/server.js",
Expand All @@ -17,7 +17,7 @@
"test": "echo \"Error: no test specified\" && exit 1",
"build": "tsc",
"lint": "eslint . --no-fix --max-warnings 0 --rule \"linebreak-style:off\"",
"sentry:sourcemaps": "sentry-cli sourcemaps inject ./dist && sentry-cli sourcemaps upload ./dist"
"sentry:sourcemaps": "sentry-cli sourcemaps inject ./dist && sentry-cli sourcemaps upload --release=%npm_package_version% ./dist"
},
"keywords": [
"bot",
Expand All @@ -41,6 +41,7 @@
"@typescript-eslint/eslint-plugin": "^5.39.0",
"@typescript-eslint/parser": "^5.39.0",
"eslint": "^8.45.0",
"eslint-config-alloy": "^5.1.2",
"eslint-import-resolver-typescript": "^3.3.0",
"eslint-plugin-import": "^2.27.5",
"ts-node": "^10.9.1",
Expand Down
4 changes: 3 additions & 1 deletion resources/manifest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ oauth_config:
scopes:
bot:
- channels:history
- channels:join
- chat:write
- chat:write.public
- commands
- im:history
- reactions:write
- chat:write.public
- channels:manage
settings:
event_subscriptions:
request_url: https://lounasbotti-disec.fly.dev/slack/events
Expand Down
4 changes: 2 additions & 2 deletions src/AdminEvents.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import bolt from "@slack/bolt";
import { Settings } from "./model/Settings.js";
import type bolt from "@slack/bolt";
import type { Settings } from "./model/Settings.js";

const ANNOUNCE_REGEXP = /lounasbotti\sadmin\sannounce\s"((?:\w|\d){1,30})"\s"([^"]{1,2000})"/;

Expand Down
15 changes: 8 additions & 7 deletions src/BlockParsers.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Bits, BlockCollection, Blocks, Elements, HomeTab, Md, setIfTruthy, user } from "slack-block-builder";
import { SlackBlockDto, SlackHomeTabDto } from "slack-block-builder/dist/internal";
import type { SlackBlockDto, SlackHomeTabDto } from "slack-block-builder/dist/internal";

import { LounasDataProvider, LounasResponse } from "./model/dataProviders/LounasDataProvider.js";
import { RestaurantNameMap, Settings } from "./model/Settings.js";
import type { LounasResponse } from "./model/dataProviders/LounasDataProvider.js";
import type { Settings } from "./model/Settings.js";
import { RestaurantNameMap } from "./model/Settings.js";

export default class BlockParsers {
private static limitVotesToOneOptionBit = Bits.Option({ text: "Salli käyttäjän äänestää vain yhtä vaihtoehtoa" });
Expand Down Expand Up @@ -42,19 +43,19 @@ export default class BlockParsers {
];
}

public static parseHomeTabView(data: { settings: Settings, version: string, userId: string}): Readonly<SlackHomeTabDto> {
public static parseHomeTabView(data: { settings: Settings, userId: string}): Readonly<SlackHomeTabDto> {
const debugInformation: string[] = [
data.settings.configSource ? `Config loaded from ${data.settings.configSource}` : null,
`Data provider: ${(data.settings.dataProvider as LounasDataProvider).id} (${(data.settings.dataProvider as LounasDataProvider).baseUrl})`,
`Data provider: ${data.settings.dataProvider.id} (${data.settings.dataProvider.baseUrl})`,
`[LounasEmoji] ${data.settings.emojiRules?.size ? `${data.settings.emojiRules?.size} regular expressions successfully loaded` : "No rules loaded"}`,
global.LOUNASBOTTI_JOBS.cacheClearing ? `Cached data will be cleared at ${global.LOUNASBOTTI_JOBS.cacheClearing.nextInvocation().toLocaleString("en-US")}` : null,
global.LOUNASBOTTI_JOBS.prefetch ? `Automatic posting to subscribed channels will next occur at ${global.LOUNASBOTTI_JOBS.prefetch.nextInvocation().toLocaleString("en-US")}` : null,
global.LOUNASBOTTI_JOBS.subscriptions ? `Automatic posting to subscribed channels will next occur at ${global.LOUNASBOTTI_JOBS.subscriptions.nextInvocation().toLocaleString("en-US")}` : null,
`${(data.settings.subscribedChannels || []).length} channel subscription(s)`
].filter(Boolean) as string[];

return HomeTab()
.blocks(
Blocks.Header({ text: `Lounasbotti V${data.version}` }).end(),
Blocks.Header({ text: `Lounasbotti ${global.LOUNASBOTTI_VERSION}` }).end(),
Blocks.Section({ text: Md.italic("By <https://github.com/ojaha065|Jani Haiko>") }).end(),
Blocks.Divider().end(),

Expand Down
5 changes: 3 additions & 2 deletions src/BotActions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import bolt, { BlockAction, CheckboxesAction, PlainTextInputAction } from "@slack/bolt";
import { Settings } from "./model/Settings.js";
import type { BlockAction, CheckboxesAction, PlainTextInputAction } from "@slack/bolt";
import type bolt from "@slack/bolt";
import type { Settings } from "./model/Settings.js";
import * as SettingsRepository from "./model/SettingsRepository.js";

export default function(app: bolt.App, settings: Settings) {
Expand Down
19 changes: 15 additions & 4 deletions src/BotCommands.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import bolt from "@slack/bolt";
import type bolt from "@slack/bolt";
import { Md } from "slack-block-builder";
import { Settings } from "./model/Settings";
import type { Settings } from "./model/Settings";
import * as SettingsRepository from "./model/SettingsRepository.js";

export default function(app: bolt.App, settings: Settings) {
Expand Down Expand Up @@ -53,7 +53,16 @@ export default function(app: bolt.App, settings: Settings) {
return;
}

// TODO: Check rights to channel
try {
await args.client.conversations.join({ channel: args.body.channel_id });
} catch (error) {
console.error(error);
args.respond({
response_type: "ephemeral",
text: "Error subscribing to channel. Lounasbotti could not join this channel. Please contact support."
});
return;
}

SettingsRepository.update({
instanceId: settings.instanceId,
Expand All @@ -65,7 +74,7 @@ export default function(app: bolt.App, settings: Settings) {
console.info(`User ${args.body.user_id} (${args.body.user_name}) subscribed to channel ${args.body.channel_id}`);
args.respond({
response_type: "in_channel",
text: `${Md.user(args.body.user_id)} subscribed Lounasbotti to this channel. Next automatic activation will happen at ${Md.codeInline(global.LOUNASBOTTI_JOBS.prefetch.nextInvocation().toLocaleString("en-US"))}. Use ${Md.codeInline("/lounasbotti unsubscribe")} to unsubscribe.`
text: `${Md.user(args.body.user_id)} subscribed Lounasbotti to this channel. Next automatic activation will happen at ${Md.codeInline(global.LOUNASBOTTI_JOBS.subscriptions.nextInvocation().toLocaleString("en-US"))}. Use ${Md.codeInline("/lounasbotti unsubscribe")} to unsubscribe.`
});
}).catch(error => {
console.error(error);
Expand Down Expand Up @@ -99,6 +108,8 @@ export default function(app: bolt.App, settings: Settings) {
response_type: "in_channel",
text: `${Md.user(args.body.user_id)} unsubscribed Lounasbotti from this channel. Use ${Md.codeInline("/lounasbotti subscribe")} to subscribe again.`
});

args.client.conversations.leave({ channel: args.body.channel_id });
}).catch(error => {
console.error(error);
args.respond({
Expand Down
Loading

0 comments on commit fb53ea0

Please sign in to comment.