mirror of
https://github.com/CHN-beta/nixos.git
synced 2026-01-11 16:49:22 +08:00
packages/misskey-forwarder: init
This commit is contained in:
@@ -144,6 +144,7 @@ inputs: rec
|
||||
buildProxy = inputs.pkgs.lib.mkBuildproxy ./pybinding/proxy.nix;
|
||||
};
|
||||
brokenaxes = inputs.pkgs.python3Packages.callPackage ./brokenaxes.nix { src = inputs.topInputs.brokenaxes; };
|
||||
misskey-forwarder = inputs.pkgs.callPackage ./misskey-forwarder { inherit biu; stdenv = inputs.pkgs.clang18Stdenv; };
|
||||
|
||||
fromYaml = content: builtins.fromJSON (builtins.readFile
|
||||
(inputs.pkgs.runCommand "toJSON" {}
|
||||
|
||||
1
packages/misskey-forwarder/.envrc
Normal file
1
packages/misskey-forwarder/.envrc
Normal file
@@ -0,0 +1 @@
|
||||
use flake .#misskey-forwarder
|
||||
24
packages/misskey-forwarder/CMakeLists.txt
Normal file
24
packages/misskey-forwarder/CMakeLists.txt
Normal file
@@ -0,0 +1,24 @@
|
||||
cmake_minimum_required(VERSION 3.14)
|
||||
project(misskey-forwarder VERSION 0.0.0 LANGUAGES CXX)
|
||||
enable_testing()
|
||||
include(GNUInstallDirs)
|
||||
|
||||
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
|
||||
message("Setting build type to 'Release' as none was specified.")
|
||||
set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build." FORCE)
|
||||
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo")
|
||||
endif()
|
||||
|
||||
find_package(biu REQUIRED)
|
||||
find_package(httplib REQUIRED)
|
||||
|
||||
add_executable(misskey-forwarder src/main.cpp)
|
||||
target_link_libraries(misskey-forwarder PRIVATE biu::biu httplib::httplib)
|
||||
target_compile_features(misskey-forwarder PRIVATE cxx_std_23)
|
||||
target_compile_definitions(misskey-forwarder PRIVATE FORWARDER_CONFIG_FILE="${FORWARDER_CONFIG_FILE}")
|
||||
install(TARGETS misskey-forwarder RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
|
||||
|
||||
get_property(ImportedTargets DIRECTORY "${CMAKE_SOURCE_DIR}" PROPERTY IMPORTED_TARGETS)
|
||||
message("Imported targets: ${ImportedTargets}")
|
||||
message("List of compile features: ${CMAKE_CXX_COMPILE_FEATURES}")
|
||||
message("CMake build type: ${CMAKE_BUILD_TYPE}")
|
||||
8
packages/misskey-forwarder/default.nix
Normal file
8
packages/misskey-forwarder/default.nix
Normal file
@@ -0,0 +1,8 @@
|
||||
{ lib, stdenv, cmake, pkg-config, biu, configFile ? null, httplib }: stdenv.mkDerivation
|
||||
{
|
||||
name = "misskey-forwarder";
|
||||
src = ./.;
|
||||
buildInputs = [ biu httplib ];
|
||||
nativeBuildInputs = [ cmake pkg-config ];
|
||||
cmakeFlags = lib.optional (configFile != null) [ "-DFORWARDER_CONFIG_FILE=${configFile}" ];
|
||||
}
|
||||
60
packages/misskey-forwarder/src/main.cpp
Normal file
60
packages/misskey-forwarder/src/main.cpp
Normal file
@@ -0,0 +1,60 @@
|
||||
# include <biu.hpp>
|
||||
# include <httplib.h>
|
||||
# include <tgbot/tgbot.h>
|
||||
# ifndef FORWARDER_CONFIG_FILE
|
||||
# define FORWARDER_CONFIG_FILE "./config.yml"
|
||||
# endif
|
||||
|
||||
int main()
|
||||
{
|
||||
using namespace biu::literals;
|
||||
biu::Logger::Guard log;
|
||||
|
||||
struct Config
|
||||
{
|
||||
std::string Secret;
|
||||
std::string TelegramBotToken;
|
||||
std::string TelegramChatId;
|
||||
int ServerPort;
|
||||
};
|
||||
auto config = YAML::LoadFile(FORWARDER_CONFIG_FILE).as<Config>();
|
||||
|
||||
biu::Logger::try_exec([&]
|
||||
{
|
||||
httplib::Server svr;
|
||||
|
||||
svr.Post("/", [&](const httplib::Request& req, httplib::Response& res)
|
||||
{
|
||||
biu::Logger::try_exec([&]
|
||||
{
|
||||
if (req.get_header_value("x-misskey-hook-secret") != config.Secret)
|
||||
throw std::runtime_error("Invalid secret key.");
|
||||
|
||||
struct Content
|
||||
{
|
||||
std::string type, server;
|
||||
struct { struct
|
||||
{
|
||||
std::string text, visibility;
|
||||
struct Renote { std::string id; };
|
||||
std::optional<Renote> renote;
|
||||
} note; } body;
|
||||
};
|
||||
auto content = YAML::Load(req.body).as<Content>();
|
||||
|
||||
if (content.type != "note" || content.body.note.visibility != "public") return;
|
||||
std::string text = content.body.note.text;
|
||||
if (content.body.note.renote)
|
||||
text += "\n🔁 Renote: https://{}/notes/{}"_f(content.server, content.body.note.renote->id);
|
||||
|
||||
TgBot::Bot bot(config.TelegramBotToken);
|
||||
bot.getApi().sendMessage(config.TelegramChatId, text);
|
||||
|
||||
res.status = 200;
|
||||
res.body = "OK";
|
||||
});
|
||||
});
|
||||
svr.listen("0.0.0.0", config.ServerPort);
|
||||
return 0;
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user