1
0
mirror of https://github.com/flarum/core.git synced 2025-08-04 23:47:32 +02:00

Merge remote-tracking branch 'extensions_akismet/REWRITE'

This commit is contained in:
Alexander Skvortsov
2022-03-11 18:01:09 -05:00
39 changed files with 3854 additions and 0 deletions

View File

@@ -0,0 +1,19 @@
# EditorConfig helps developers define and maintain consistent
# coding styles between different editors and IDEs
# editorconfig.org
root = true
[*]
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
indent_style = space
indent_size = 2
[*.{diff,md}]
trim_trailing_whitespace = false
[*.{php,xml,json}]
indent_size = 4

18
extensions/akismet/.gitattributes vendored Normal file
View File

@@ -0,0 +1,18 @@
.gitattributes export-ignore
.gitignore export-ignore
.gitmodules export-ignore
.github export-ignore
.travis export-ignore
.travis.yml export-ignore
.editorconfig export-ignore
.styleci.yml export-ignore
phpunit.xml export-ignore
tests export-ignore
js/dist/* -diff
js/dist/* linguist-generated
js/dist-typings/* linguist-generated
js/yarn.lock -diff
* text=auto eol=lf

View File

@@ -0,0 +1,15 @@
name: Akismet PHP
on: [workflow_dispatch, push, pull_request]
# The reusable workflow definitions will be moved to the `flarum/framework` repo soon.
# This will break your current script.
# When this happens, run `flarum-cli audit infra --fix` to update your infrastructure.
jobs:
run:
uses: flarum/.github/.github/workflows/REUSABLE_backend.yml@main
with:
enable_backend_testing: true
backend_directory: .

View File

@@ -0,0 +1,21 @@
name: Akismet JS
on: [workflow_dispatch, push, pull_request]
# The reusable workflow definitions will be moved to the `flarum/framework` repo soon.
# This will break your current script.
# When this happens, run `flarum-cli audit infra --fix` to update your infrastructure.
jobs:
run:
uses: flarum/.github/.github/workflows/REUSABLE_frontend.yml@main
with:
enable_bundlewatch: false
enable_prettier: true
enable_typescript: true
frontend_directory: ./js
main_git_branch: master
secrets:
bundlewatch_github_token: ${{ secrets.BUNDLEWATCH_GITHUB_TOKEN }}

12
extensions/akismet/.gitignore vendored Normal file
View File

@@ -0,0 +1,12 @@
/vendor
composer.lock
composer.phar
.DS_Store
Thumbs.db
tests/.phpunit.result.cache
/tests/integration/tmp
.vagrant
.idea/*
.vscode
js/coverage-ts

View File

@@ -0,0 +1,14 @@
preset: recommended
enabled:
- logical_not_operators_with_successor_space
disabled:
- align_double_arrow
- blank_line_after_opening_tag
- multiline_array_trailing_comma
- new_with_braces
- phpdoc_align
- phpdoc_order
- phpdoc_separation
- phpdoc_types

View File

@@ -0,0 +1,62 @@
# Changelog
## [1.2.0](https://github.com/flarum/akismet/compare/v1.1.0...v1.2.0)
### Added
- Permission to bypass Akismet (https://github.com/flarum/akismet/pull/24)
- Option to remove blatant spam (https://github.com/flarum/akismet/pull/24)
- Sending additional parameters like is_test (https://github.com/flarum/akismet/pull/24)
### Changed
- Convert frontend to TypeScript (https://github.com/flarum/akismet/pull/24)
- Call Akismet API manually (https://github.com/flarum/akismet/pull/24)
### Fixed
- PHP 8 compatibility (https://github.com/flarum/akismet/pull/24)
## [1.1.0](https://github.com/flarum/akismet/compare/v1.0.0...v1.1.0)
No changes.
## [1.0.0](https://github.com/flarum/akismet/compare/v0.1.0-beta.16...v1.0.0)
### Changed
- Compatibility with Flarum v1.0.0.
## [0.1.0-beta.16](https://github.com/flarum/akismet/compare/v0.1.0-beta.15...v0.1.0-beta.16)
### Changed
- Updated admin category from moderation to feature (https://github.com/flarum/akismet/pull/18)
- Moved locale files from translation pack to extension (https://github.com/flarum/akismet/pull/17).
## [0.1.0-beta.15](https://github.com/flarum/akismet/compare/v0.1.0-beta.14.1...v0.1.0-beta.15)
### Changed
- Updated composer.json and admin javascript for new admin area.
- Updated extend.php with latest extenders.
## [0.1.0-beta.14.1](https://github.com/flarum/akismet/compare/v0.1.0-beta.14...v0.1.0-beta.14.1)
### Fixed
- Incorrect use of Container::url method
## [0.1.0-beta.14](https://github.com/flarum/akismet/compare/v0.1.0-beta.13...v0.1.0-beta.14)
### Changed
- Updated mithril to version 2
- Load language strings correctly on en-/disable
- Updated JS dependencies
## [0.1.0-beta.13](https://github.com/flarum/akismet/compare/v0.1.0-beta.12...v0.1.0-beta.13)
### Changed
- Updated JS dependencies
## [0.1.0-beta.9](https://github.com/flarum/akismet/compare/v0.1.0-beta.8...v0.1.0-beta.9)
### Changed
- Replace event subscribers (that resolve services too early) with listeners ([cf3f724](https://github.com/flarum/akismet/commit/cf3f724bafbcf901a41cb76dfe44c896debde241))

View File

@@ -0,0 +1,22 @@
The MIT License (MIT)
Copyright (c) 2019-2021 Stichting Flarum (Flarum Foundation)
Copyright (c) 2014-2019 Toby Zerner (toby.zerner@gmail.com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@@ -0,0 +1,81 @@
{
"name": "flarum/akismet",
"description": "Stop spam using the Akismet anti-spam service.",
"type": "flarum-extension",
"keywords": [
"moderation"
],
"license": "MIT",
"support": {
"issues": "https://github.com/flarum/core/issues",
"source": "https://github.com/flarum/akismet",
"forum": "https://discuss.flarum.org"
},
"homepage": "https://flarum.org",
"funding": [
{
"type": "website",
"url": "https://flarum.org/donate/"
}
],
"require": {
"flarum/core": "^1.2",
"flarum/approval": "^1.2",
"guzzlehttp/guzzle": "^7.4"
},
"autoload": {
"psr-4": {
"Flarum\\Akismet\\": "src/"
}
},
"extra": {
"branch-alias": {
"dev-master": "1.x-dev"
},
"flarum-extension": {
"title": "Akismet",
"category": "feature",
"icon": {
"image": "icon.jpg",
"backgroundSize": "cover",
"backgroundColor": "#5e8e14",
"color": "#fff"
}
},
"flarum-cli": {
"modules": {
"js": true,
"jsCommon": false,
"gitConf": true,
"githubActions": true,
"prettier": true,
"typescript": true,
"bundlewatch": false,
"backendTesting": true,
"editorConfig": true,
"styleci": true,
"admin": true,
"forum": true,
"css": false
}
}
},
"scripts": {
"test": [
"@test:unit",
"@test:integration"
],
"test:unit": "phpunit -c tests/phpunit.unit.xml",
"test:integration": "phpunit -c tests/phpunit.integration.xml",
"test:setup": "@php tests/integration/setup.php"
},
"scripts-descriptions": {
"test": "Runs all tests.",
"test:unit": "Runs all unit tests.",
"test:integration": "Runs all integration tests.",
"test:setup": "Sets up a database for use with integration tests. Execute this only once."
},
"require-dev": {
"flarum/testing": "^1.0.0"
}
}

View File

@@ -0,0 +1,33 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
use Flarum\Akismet\Listener;
use Flarum\Akismet\Provider\AkismetProvider;
use Flarum\Approval\Event\PostWasApproved;
use Flarum\Extend;
use Flarum\Post\Event\Hidden;
use Flarum\Post\Event\Saving;
return [
(new Extend\Frontend('forum'))
->js(__DIR__.'/js/dist/forum.js'),
(new Extend\Frontend('admin'))
->js(__DIR__.'/js/dist/admin.js'),
new Extend\Locales(__DIR__.'/locale'),
(new Extend\Event())
->listen(Hidden::class, Listener\SubmitSpam::class)
->listen(PostWasApproved::class, Listener\SubmitHam::class)
->listen(Saving::class, Listener\ValidatePost::class),
(new Extend\ServiceProvider())
->register(AkismetProvider::class),
];

BIN
extensions/akismet/icon.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.0 KiB

9
extensions/akismet/js/.gitignore vendored Normal file
View File

@@ -0,0 +1,9 @@
.pnp.*
.yarn/*
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/sdks
!.yarn/versions
node_modules

View File

@@ -0,0 +1 @@
export * from './src/admin';

View File

@@ -0,0 +1 @@
export {};

View File

@@ -0,0 +1 @@
export {};

2
extensions/akismet/js/dist/admin.js generated vendored Normal file
View File

@@ -0,0 +1,2 @@
(()=>{var e={n:t=>{var a=t&&t.__esModule?()=>t.default:()=>t;return e.d(a,{a}),a},d:(t,a)=>{for(var r in a)e.o(a,r)&&!e.o(t,r)&&Object.defineProperty(t,r,{enumerable:!0,get:a[r]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r:e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},t={};(()=>{"use strict";e.r(t);const a=flarum.core.compat["admin/app"];var r=e.n(a);r().initializers.add("flarum-akismet",(function(){r().extensionData.for("flarum-akismet").registerSetting({setting:"flarum-akismet.api_key",type:"text",label:r().translator.trans("flarum-akismet.admin.akismet_settings.api_key_label")}).registerSetting({setting:"flarum-akismet.delete_blatant_spam",type:"boolean",label:r().translator.trans("flarum-akismet.admin.akismet_settings.delete_blatant_spam_label"),help:r().translator.trans("flarum-akismet.admin.akismet_settings.delete_blatant_spam_help")}).registerPermission({icon:"fas fa-vote-yea",label:r().translator.trans("flarum-akismet.admin.permissions.bypass_akismet"),permission:"bypassAkismet"},"start")}))})(),module.exports=t})();
//# sourceMappingURL=admin.js.map

1
extensions/akismet/js/dist/admin.js.map generated vendored Normal file
View File

@@ -0,0 +1 @@
{"version":3,"file":"admin.js","mappings":"MACA,IAAIA,EAAsB,CCA1BA,EAAyBC,IACxB,IAAIC,EAASD,GAAUA,EAAOE,WAC7B,IAAOF,EAAiB,QACxB,IAAM,EAEP,OADAD,EAAoBI,EAAEF,EAAQ,CAAEG,IACzBH,GCLRF,EAAwB,CAACM,EAASC,KACjC,IAAI,IAAIC,KAAOD,EACXP,EAAoBS,EAAEF,EAAYC,KAASR,EAAoBS,EAAEH,EAASE,IAC5EE,OAAOC,eAAeL,EAASE,EAAK,CAAEI,YAAY,EAAMC,IAAKN,EAAWC,MCJ3ER,EAAwB,CAACc,EAAKC,IAAUL,OAAOM,UAAUC,eAAeC,KAAKJ,EAAKC,GCClFf,EAAyBM,IACH,oBAAXa,QAA0BA,OAAOC,aAC1CV,OAAOC,eAAeL,EAASa,OAAOC,YAAa,CAAEC,MAAO,WAE7DX,OAAOC,eAAeL,EAAS,aAAc,CAAEe,OAAO,M,+BCLvD,MAAM,EAA+BC,OAAOC,KAAKC,OAAO,a,aCExDC,IAAAA,aAAAA,IAAqB,kBAAkB,WACrCA,IAAAA,cAAAA,IACO,kBACJC,gBAAgB,CACfC,QAAS,yBACTC,KAAM,OACNC,MAAOJ,IAAAA,WAAAA,MAAqB,yDAE7BC,gBAAgB,CAEfC,QAAS,qCACTC,KAAM,UACNC,MAAOJ,IAAAA,WAAAA,MAAqB,mEAC5BK,KAAML,IAAAA,WAAAA,MAAqB,oEAE5BM,mBACC,CACEC,KAAM,kBACNH,MAAOJ,IAAAA,WAAAA,MAAqB,mDAC5BQ,WAAY,iBAEd,a","sources":["webpack://@flarum/akismet/webpack/bootstrap","webpack://@flarum/akismet/webpack/runtime/compat get default export","webpack://@flarum/akismet/webpack/runtime/define property getters","webpack://@flarum/akismet/webpack/runtime/hasOwnProperty shorthand","webpack://@flarum/akismet/webpack/runtime/make namespace object","webpack://@flarum/akismet/external root \"flarum.core.compat['admin/app']\"","webpack://@flarum/akismet/./src/admin/index.ts"],"sourcesContent":["// The require scope\nvar __webpack_require__ = {};\n\n","// getDefaultExport function for compatibility with non-harmony modules\n__webpack_require__.n = (module) => {\n\tvar getter = module && module.__esModule ?\n\t\t() => (module['default']) :\n\t\t() => (module);\n\t__webpack_require__.d(getter, { a: getter });\n\treturn getter;\n};","// define getter functions for harmony exports\n__webpack_require__.d = (exports, definition) => {\n\tfor(var key in definition) {\n\t\tif(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {\n\t\t\tObject.defineProperty(exports, key, { enumerable: true, get: definition[key] });\n\t\t}\n\t}\n};","__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))","// define __esModule on exports\n__webpack_require__.r = (exports) => {\n\tif(typeof Symbol !== 'undefined' && Symbol.toStringTag) {\n\t\tObject.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });\n\t}\n\tObject.defineProperty(exports, '__esModule', { value: true });\n};","const __WEBPACK_NAMESPACE_OBJECT__ = flarum.core.compat['admin/app'];","import app from 'flarum/admin/app';\n\napp.initializers.add('flarum-akismet', () => {\n app.extensionData\n .for('flarum-akismet')\n .registerSetting({\n setting: 'flarum-akismet.api_key',\n type: 'text',\n label: app.translator.trans('flarum-akismet.admin.akismet_settings.api_key_label'),\n })\n .registerSetting({\n //https://blog.akismet.com/2014/04/23/theres-a-ninja-in-your-akismet/\n setting: 'flarum-akismet.delete_blatant_spam',\n type: 'boolean',\n label: app.translator.trans('flarum-akismet.admin.akismet_settings.delete_blatant_spam_label'),\n help: app.translator.trans('flarum-akismet.admin.akismet_settings.delete_blatant_spam_help'),\n })\n .registerPermission(\n {\n icon: 'fas fa-vote-yea',\n label: app.translator.trans('flarum-akismet.admin.permissions.bypass_akismet'),\n permission: 'bypassAkismet',\n },\n 'start'\n );\n});\n"],"names":["__webpack_require__","module","getter","__esModule","d","a","exports","definition","key","o","Object","defineProperty","enumerable","get","obj","prop","prototype","hasOwnProperty","call","Symbol","toStringTag","value","flarum","core","compat","app","registerSetting","setting","type","label","help","registerPermission","icon","permission"],"sourceRoot":""}

2
extensions/akismet/js/dist/forum.js generated vendored Normal file
View File

@@ -0,0 +1,2 @@
(()=>{var t={n:e=>{var o=e&&e.__esModule?()=>e.default:()=>e;return t.d(o,{a:o}),o},d:(e,o)=>{for(var r in o)t.o(o,r)&&!t.o(e,r)&&Object.defineProperty(e,r,{enumerable:!0,get:o[r]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e),r:t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})}},e={};(()=>{"use strict";t.r(e);const o=flarum.core.compat["common/extend"],r=flarum.core.compat["forum/app"];var a=t.n(r);const n=flarum.core.compat["forum/utils/PostControls"];var m=t.n(n);const s=flarum.core.compat["forum/components/CommentPost"];var u=t.n(s);a().initializers.add("flarum-akismet",(function(){(0,o.extend)(m(),"destructiveControls",(function(t,e){if(t.has("approve")){var o=e.flags();o&&o.some((function(t){return"akismet"===t.type()}))&&(t.get("approve").children=a().translator.trans("flarum-akismet.forum.post.not_spam_button"))}})),(0,o.override)(u().prototype,"flagReason",(function(t,e){return"akismet"===e.type()?a().translator.trans("flarum-akismet.forum.post.akismet_flagged_text"):t(e)}))}))})(),module.exports=e})();
//# sourceMappingURL=forum.js.map

1
extensions/akismet/js/dist/forum.js.map generated vendored Normal file
View File

@@ -0,0 +1 @@
{"version":3,"file":"forum.js","mappings":"MACA,IAAIA,EAAsB,CCA1BA,EAAyBC,IACxB,IAAIC,EAASD,GAAUA,EAAOE,WAC7B,IAAOF,EAAiB,QACxB,IAAM,EAEP,OADAD,EAAoBI,EAAEF,EAAQ,CAAEG,EAAGH,IAC5BA,GCLRF,EAAwB,CAACM,EAASC,KACjC,IAAI,IAAIC,KAAOD,EACXP,EAAoBS,EAAEF,EAAYC,KAASR,EAAoBS,EAAEH,EAASE,IAC5EE,OAAOC,eAAeL,EAASE,EAAK,CAAEI,YAAY,EAAMC,IAAKN,EAAWC,MCJ3ER,EAAwB,CAACc,EAAKC,IAAUL,OAAOM,UAAUC,eAAeC,KAAKJ,EAAKC,GCClFf,EAAyBM,IACH,oBAAXa,QAA0BA,OAAOC,aAC1CV,OAAOC,eAAeL,EAASa,OAAOC,YAAa,CAAEC,MAAO,WAE7DX,OAAOC,eAAeL,EAAS,aAAc,CAAEe,OAAO,M,+BCLvD,MAAM,EAA+BC,OAAOC,KAAKC,OAAO,iBCAlD,EAA+BF,OAAOC,KAAKC,OAAO,a,aCAxD,MAAM,EAA+BF,OAAOC,KAAKC,OAAO,4B,aCAxD,MAAM,EAA+BF,OAAOC,KAAKC,OAAO,gC,aCQxDC,IAAAA,aAAAA,IAAqB,kBAAkB,YACrCC,EAAAA,EAAAA,QAAOC,IAAc,uBAAuB,SAAUC,EAAiBC,GACrE,GAAID,EAAME,IAAI,WAAY,CACxB,IAAMC,EAAQF,EAAKE,QAEfA,GAASA,EAAMC,MAAK,SAACC,GAAD,MAA0B,YAAhBA,EAAKC,YACrCN,EAAMf,IAAI,WAAWsB,SAAWV,IAAAA,WAAAA,MAAqB,mDAK3DW,EAAAA,EAAAA,UAASC,IAAAA,UAAuB,cAAc,SAAUC,EAAUL,GAChE,MAAoB,YAAhBA,EAAKC,OACAT,IAAAA,WAAAA,MAAqB,kDAGvBa,EAASL,U","sources":["webpack://@flarum/akismet/webpack/bootstrap","webpack://@flarum/akismet/webpack/runtime/compat get default export","webpack://@flarum/akismet/webpack/runtime/define property getters","webpack://@flarum/akismet/webpack/runtime/hasOwnProperty shorthand","webpack://@flarum/akismet/webpack/runtime/make namespace object","webpack://@flarum/akismet/external root \"flarum.core.compat['common/extend']\"","webpack://@flarum/akismet/external root \"flarum.core.compat['forum/app']\"","webpack://@flarum/akismet/external root \"flarum.core.compat['forum/utils/PostControls']\"","webpack://@flarum/akismet/external root \"flarum.core.compat['forum/components/CommentPost']\"","webpack://@flarum/akismet/./src/forum/index.ts"],"sourcesContent":["// The require scope\nvar __webpack_require__ = {};\n\n","// getDefaultExport function for compatibility with non-harmony modules\n__webpack_require__.n = (module) => {\n\tvar getter = module && module.__esModule ?\n\t\t() => (module['default']) :\n\t\t() => (module);\n\t__webpack_require__.d(getter, { a: getter });\n\treturn getter;\n};","// define getter functions for harmony exports\n__webpack_require__.d = (exports, definition) => {\n\tfor(var key in definition) {\n\t\tif(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {\n\t\t\tObject.defineProperty(exports, key, { enumerable: true, get: definition[key] });\n\t\t}\n\t}\n};","__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))","// define __esModule on exports\n__webpack_require__.r = (exports) => {\n\tif(typeof Symbol !== 'undefined' && Symbol.toStringTag) {\n\t\tObject.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });\n\t}\n\tObject.defineProperty(exports, '__esModule', { value: true });\n};","const __WEBPACK_NAMESPACE_OBJECT__ = flarum.core.compat['common/extend'];","const __WEBPACK_NAMESPACE_OBJECT__ = flarum.core.compat['forum/app'];","const __WEBPACK_NAMESPACE_OBJECT__ = flarum.core.compat['forum/utils/PostControls'];","const __WEBPACK_NAMESPACE_OBJECT__ = flarum.core.compat['forum/components/CommentPost'];","import { extend, override } from 'flarum/common/extend';\nimport app from 'flarum/forum/app';\n\nimport PostControls from 'flarum/forum/utils/PostControls';\nimport CommentPost from 'flarum/forum/components/CommentPost';\nimport ItemList from 'flarum/common/utils/ItemList';\nimport Post from 'flarum/common/models/Post';\n\napp.initializers.add('flarum-akismet', () => {\n extend(PostControls, 'destructiveControls', function (items: ItemList, post: Post) {\n if (items.has('approve')) {\n const flags = post.flags();\n\n if (flags && flags.some((flag) => flag.type() === 'akismet')) {\n items.get('approve').children = app.translator.trans('flarum-akismet.forum.post.not_spam_button');\n }\n }\n });\n\n override(CommentPost.prototype, 'flagReason', function (original, flag) {\n if (flag.type() === 'akismet') {\n return app.translator.trans('flarum-akismet.forum.post.akismet_flagged_text');\n }\n\n return original(flag);\n });\n});\n"],"names":["__webpack_require__","module","getter","__esModule","d","a","exports","definition","key","o","Object","defineProperty","enumerable","get","obj","prop","prototype","hasOwnProperty","call","Symbol","toStringTag","value","flarum","core","compat","app","extend","PostControls","items","post","has","flags","some","flag","type","children","override","CommentPost","original"],"sourceRoot":""}

View File

@@ -0,0 +1 @@
export * from './src/forum';

View File

@@ -0,0 +1,26 @@
{
"private": true,
"name": "@flarum/akismet",
"prettier": "@flarum/prettier-config",
"scripts": {
"dev": "webpack --mode development --watch",
"build": "webpack --mode production",
"analyze": "cross-env ANALYZER=true yarn build",
"format": "prettier --write src",
"format-check": "prettier --check src",
"check-typings": "tsc --noEmit --emitDeclarationOnly false",
"check-typings-coverage": "typescript-coverage-report",
"clean-typings": "npx rimraf dist-typings && mkdir dist-typings",
"build-typings": "npm run clean-typings && tsc && [ -e src/@types ] && cp -r src/@types dist-typings/@types"
},
"devDependencies": {
"@flarum/prettier-config": "^1.0.0",
"flarum-tsconfig": "^1.0.2",
"prettier": "^2.5.1",
"flarum-webpack-config": "^2.0.0",
"webpack": "^5.65.0",
"webpack-cli": "^4.9.1",
"typescript": "^4.5.4",
"typescript-coverage-report": "^0.6.1"
}
}

View File

@@ -0,0 +1,26 @@
import app from 'flarum/admin/app';
app.initializers.add('flarum-akismet', () => {
app.extensionData
.for('flarum-akismet')
.registerSetting({
setting: 'flarum-akismet.api_key',
type: 'text',
label: app.translator.trans('flarum-akismet.admin.akismet_settings.api_key_label'),
})
.registerSetting({
//https://blog.akismet.com/2014/04/23/theres-a-ninja-in-your-akismet/
setting: 'flarum-akismet.delete_blatant_spam',
type: 'boolean',
label: app.translator.trans('flarum-akismet.admin.akismet_settings.delete_blatant_spam_label'),
help: app.translator.trans('flarum-akismet.admin.akismet_settings.delete_blatant_spam_help'),
})
.registerPermission(
{
icon: 'fas fa-vote-yea',
label: app.translator.trans('flarum-akismet.admin.permissions.bypass_akismet'),
permission: 'bypassAkismet',
},
'start'
);
});

View File

@@ -0,0 +1,27 @@
import { extend, override } from 'flarum/common/extend';
import app from 'flarum/forum/app';
import PostControls from 'flarum/forum/utils/PostControls';
import CommentPost from 'flarum/forum/components/CommentPost';
import ItemList from 'flarum/common/utils/ItemList';
import Post from 'flarum/common/models/Post';
app.initializers.add('flarum-akismet', () => {
extend(PostControls, 'destructiveControls', function (items: ItemList, post: Post) {
if (items.has('approve')) {
const flags = post.flags();
if (flags && flags.some((flag) => flag.type() === 'akismet')) {
items.get('approve').children = app.translator.trans('flarum-akismet.forum.post.not_spam_button');
}
}
});
override(CommentPost.prototype, 'flagReason', function (original, flag) {
if (flag.type() === 'akismet') {
return app.translator.trans('flarum-akismet.forum.post.akismet_flagged_text');
}
return original(flag);
});
});

View File

@@ -0,0 +1,16 @@
{
// Use Flarum's tsconfig as a starting point
"extends": "flarum-tsconfig",
// This will match all .ts, .tsx, .d.ts, .js, .jsx files in your `src` folder
// and also tells your Typescript server to read core's global typings for
// access to `dayjs` and `$` in the global namespace.
"include": ["src/**/*", "../vendor/flarum/core/js/dist-typings/@types/**/*", "@types/**/*"],
"compilerOptions": {
// This will output typings to `dist-typings`
"declarationDir": "./dist-typings",
"baseUrl": ".",
"paths": {
"flarum/*": ["../vendor/flarum/core/js/dist-typings/*"]
}
}
}

View File

@@ -0,0 +1 @@
module.exports = require('flarum-webpack-config')();

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,26 @@
flarum-akismet:
##
# UNIQUE KEYS - The following keys are used in only one location each.
##
# Translations in this namespace are used by the admin interface.
admin:
# These translations are used in the Akismet Settings.
akismet_settings:
api_key_label: API Key
title: Akismet Settings
delete_blatant_spam_label: Automatically delete blatant spam
delete_blatant_spam_help: If Akismet has determined that the comment is blatant spam, instead of flagging, automatically delete post
permissions:
bypass_akismet: Bypass Akismet
# Translations in this namespace are used by the forum user interface.
forum:
# These translations are used by the frame displayed around flagged posts.
post:
akismet_flagged_text: Akismet flagged as spam
not_spam_button: Not Spam

View File

@@ -0,0 +1,14 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
use Flarum\Database\Migration;
return Migration::addColumns('posts', [
'is_spam' => ['boolean', 'default' => 0]
]);

13
extensions/akismet/node_modules/.yarn-integrity generated vendored Normal file
View File

@@ -0,0 +1,13 @@
{
"systemParams": "linux-x64-93",
"modulesFolders": [],
"flags": [],
"linkedModules": [
"flarum-webpack-config",
"mem-fs"
],
"topLevelPatterns": [],
"lockfileEntries": {},
"files": [],
"artifacts": {}
}

View File

@@ -0,0 +1,229 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Akismet;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Psr\Http\Message\ResponseInterface;
class Akismet
{
private $apiKey;
private $apiUrl;
private $flarumVersion;
private $extensionVersion;
private $params = [];
public function __construct(string $apiKey, string $homeUrl, string $flarumVersion, string $extensionVersion, bool $inDebugMode = false)
{
$this->apiKey = $apiKey;
$this->apiUrl = "https://$apiKey.rest.akismet.com/1.1";
$this->params['blog'] = $homeUrl;
$this->flarumVersion = $flarumVersion;
$this->extensionVersion = $extensionVersion;
if ($inDebugMode) {
$this->params['is_test'] = true;
}
}
public function isConfigured(): bool
{
return ! empty($this->apiKey);
}
/**
* @param string $type e.g. comment-check, submit-spam or submit-ham;
* @throws GuzzleException
*/
protected function sendRequest(string $type): ResponseInterface
{
$client = new Client();
return $client->request('POST', "$this->apiUrl/$type", [
'headers' => [
'User-Agent' => "Flarum/$this->flarumVersion | Akismet/$this->extensionVersion",
],
'form_params' => $this->params,
]);
}
/**
* @throws GuzzleException
*/
public function checkSpam(): array
{
$response = $this->sendRequest('comment-check');
return [
'isSpam' => $response->getBody()->getContents() === 'true',
'proTip' => $response->getHeaderLine('X-akismet-pro-tip'),
];
}
/**
* @throws GuzzleException
*/
public function submitSpam()
{
$this->sendRequest('submit-spam');
}
/**
* @throws GuzzleException
*/
public function submitHam()
{
$this->sendRequest('submit-ham');
}
/**
* Allows you to set additional parameter
* This lets you use Akismet features not supported directly in this util.
*/
public function withParam(string $key, $value): Akismet
{
$new = clone $this;
$new->params[$key] = $value;
return $new;
}
/**
* The front page or home URL of the instance making the request. For a blog or wiki this would be the front page. Note: Must be a full URI, including http://.
*/
public function withBlog(string $url): Akismet
{
return $this->withParam('blog', $url);
}
/**
* IP address of the comment submitter.
*/
public function withIp(string $ip): Akismet
{
return $this->withParam('user_ip', $ip);
}
/**
* User agent string of the web browser submitting the comment - typically the HTTP_USER_AGENT cgi variable. Not to be confused with the user agent of your Akismet library.
*/
public function withUserAgent(string $userAgent): Akismet
{
return $this->withParam('user_agent', $userAgent);
}
/**
* The content of the HTTP_REFERER header should be sent here.
*/
public function withReferrer(string $referrer): Akismet
{
return $this->withParam('referrer', $referrer);
}
/**
* The full permanent URL of the entry the comment was submitted to.
*/
public function withPermalink(string $permalink): Akismet
{
return $this->withParam('permalink', $permalink);
}
/**
* A string that describes the type of content being sent
* Examples:
* comment: A blog comment.
* forum-post: A top-level forum post.
* reply: A reply to a top-level forum post.
* blog-post: A blog post.
* contact-form: A contact form or feedback form submission.
* signup: A new user account.
* message: A message sent between just a few users.
* You may send a value not listed above if none of them accurately describe your content. This is further explained here: https://blog.akismet.com/2012/06/19/pro-tip-tell-us-your-comment_type/.
*/
public function withType(string $type): Akismet
{
return $this->withParam('comment_type', $type);
}
/**
* Name submitted with the comment.
*/
public function withAuthorName(string $name): Akismet
{
return $this->withParam('comment_author', $name);
}
/**
* Email address submitted with the comment.
*/
public function withAuthorEmail(string $email): Akismet
{
return $this->withParam('comment_author_email', $email);
}
/*
* URL submitted with comment. Only send a URL that was manually entered by the user, not an automatically generated URL like the users profile URL on your site.
*/
public function withAuthorUrl(string $url): Akismet
{
return $this->withParam('comment_author_url', $url);
}
/**
* The content that was submitted.
*/
public function withContent(string $content): Akismet
{
return $this->withParam('comment_content', $content);
}
/**
* The UTC timestamp of the creation of the comment, in ISO 8601 format. May be omitted for comment-check requests if the comment is sent to the API at the time it is created.
*/
public function withDateGmt(string $date): Akismet
{
return $this->withParam('comment_date_gmt', $date);
}
/**
* The UTC timestamp of the publication time for the post, page or thread on which the comment was posted.
*/
public function withPostModifiedDateGtm(string $date): Akismet
{
return $this->withParam('comment_post_modified_gmt', $date);
}
/**
* Indicates the language(s) in use on the blog or site, in ISO 639-1 format, comma-separated. A site with articles in English and French might use “en, fr_ca”.
*/
public function withLanguage(string $language): Akismet
{
return $this->withParam('blog_lang', $language);
}
/**
* This is an optional parameter. You can use it when submitting test queries to Akismet.
*/
public function withTest(): Akismet
{
return $this->withParam('is_test', true);
}
/**
* If you are sending content to Akismet to be rechecked, such as a post that has been edited or old pending comments that youd like to recheck, include the parameter recheck_reason with a string describing why the content is being rechecked. For example, edit.
*/
public function withRecheckReason(string $reason): Akismet
{
return $this->withParam('recheck_reason', $reason);
}
}

View File

@@ -0,0 +1,45 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Akismet\Listener;
use Flarum\Akismet\Akismet;
use Flarum\Approval\Event\PostWasApproved;
class SubmitHam
{
/**
* @var Akismet
*/
protected $akismet;
public function __construct(Akismet $akismet)
{
$this->akismet = $akismet;
}
public function handle(PostWasApproved $event)
{
if (! $this->akismet->isConfigured()) {
return;
}
$post = $event->post;
if ($post->is_spam) {
$this->akismet
->withContent($post->content)
->withIp($post->ip_address)
->withAuthorName($post->user->username)
->withAuthorEmail($post->user->email)
->withType($post->number === 1 ? 'forum-post' : 'reply')
->submitHam();
}
}
}

View File

@@ -0,0 +1,45 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Akismet\Listener;
use Flarum\Akismet\Akismet;
use Flarum\Post\Event\Hidden;
class SubmitSpam
{
/**
* @var Akismet
*/
protected $akismet;
public function __construct(Akismet $akismet)
{
$this->akismet = $akismet;
}
public function handle(Hidden $event)
{
if (! $this->akismet->isConfigured()) {
return;
}
$post = $event->post;
if ($post->is_spam) {
$this->akismet
->withContent($post->content)
->withIp($post->ip_address)
->withAuthorName($post->user->username)
->withAuthorEmail($post->user->email)
->withType($post->number === 1 ? 'forum-post' : 'reply')
->submitSpam();
}
}
}

View File

@@ -0,0 +1,89 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Akismet\Listener;
use Carbon\Carbon;
use Flarum\Akismet\Akismet;
use Flarum\Flags\Flag;
use Flarum\Post\CommentPost;
use Flarum\Post\Event\Saving;
use Flarum\Settings\SettingsRepositoryInterface;
class ValidatePost
{
/**
* @var Akismet
*/
protected $akismet;
/**
* @var SettingsRepositoryInterface
*/
private $settings;
public function __construct(Akismet $akismet, SettingsRepositoryInterface $settings)
{
$this->akismet = $akismet;
$this->settings = $settings;
}
public function handle(Saving $event)
{
if (! $this->akismet->isConfigured()) {
return;
}
$post = $event->post;
//TODO Sometimes someone posts spam when editing a post. In this case 'recheck_reason=edit' can be used when sending a request to Akismet
if ($post->exists || ! ($post instanceof CommentPost) || $post->user->hasPermission('bypassAkismet')) {
return;
}
$result = $this->akismet
->withContent($post->content)
->withAuthorName($post->user->username)
->withAuthorEmail($post->user->email)
->withType($post->number == 1 ? 'forum-post' : 'reply')
->withIp($post->ip_address)
->withUserAgent($_SERVER['HTTP_USER_AGENT'])
->checkSpam();
if ($result['isSpam']) {
$post->is_spam = true;
if ($result['proTip'] === 'discard' && $this->settings->get('flarum-akismet.delete_blatant_spam')) {
$post->hide();
$post->afterSave(function ($post) {
if ($post->number == 1) {
$post->discussion->hide();
}
});
} else {
$post->is_approved = false;
$post->afterSave(function ($post) {
if ($post->number == 1) {
$post->discussion->is_approved = false;
$post->discussion->save();
}
$flag = new Flag;
$flag->post_id = $post->id;
$flag->type = 'akismet';
$flag->created_at = Carbon::now();
$flag->save();
});
}
}
}
}

View File

@@ -0,0 +1,46 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Akismet\Provider;
use Flarum\Akismet\Akismet;
use Flarum\Extension\ExtensionManager;
use Flarum\Foundation\AbstractServiceProvider;
use Flarum\Foundation\Application;
use Flarum\Foundation\Config;
use Flarum\Http\UrlGenerator;
use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Container\Container;
class AkismetProvider extends AbstractServiceProvider
{
public function register()
{
$this->container->bind(Akismet::class, function (Container $container) {
/** @var SettingsRepositoryInterface $settings */
$settings = $container->make(SettingsRepositoryInterface::class);
/** @var UrlGenerator $url */
$url = $container->make(UrlGenerator::class);
/** @var Config $config */
$config = $container->make(Config::class);
/** @var ExtensionManager $extensions */
$extensions = $this->container->make(ExtensionManager::class);
/** @var Application $app */
$app = $container->make(Application::class);
return new Akismet(
$settings->get('flarum-akismet.api_key'),
$url->to('forum')->base(),
$app::VERSION,
$extensions->getExtension('flarum-akismet')->getVersion(),
$config->inDebugMode()
);
});
}
}

View File

View File

@@ -0,0 +1,16 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
use Flarum\Testing\integration\Setup\SetupScript;
require __DIR__.'/../../vendor/autoload.php';
$setup = new SetupScript();
$setup->run();

View File

@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="true"
stopOnFailure="false"
>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">../src/</directory>
</include>
</coverage>
<testsuites>
<testsuite name="Flarum Integration Tests">
<directory suffix="Test.php">./integration</directory>
</testsuite>
</testsuites>
</phpunit>

View File

@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">../src/</directory>
</include>
</coverage>
<testsuites>
<testsuite name="Flarum Unit Tests">
<directory suffix="Test.php">./unit</directory>
</testsuite>
</testsuites>
<listeners>
<listener class="\Mockery\Adapter\Phpunit\TestListener" />
</listeners>
</phpunit>

View File