clarkwinkelmann I had a similar problem, when an error occurs while creating a post (sync email push / pwa push etc.), the user may re-click the send button, then the problem of duplicate posts occurred.
hamedeshaghi For my solution I added a ThrottleApi extender, when a new post creating request arrived, it will query the latest post in the last 10 minutes, and check is deplicated.
use Flarum\Extend\ThrottleApi;
// prevent duplicate post
(new ThrottleApi())
->set('preventDuplicate', function (ServerRequestInterface $request) {
if (!in_array($request->getAttribute('routeName'), ['discussions.create', 'posts.create'])) {
return;
}
$actor = RequestUtil::getActor($request);
$latestPost = Post::where('user_id', $actor->id)
->where('created_at', '>=', new DateTime('-10 minutes'))
->orderBy('id', 'DESC')
->first();
if ($latestPost) {
$latestContent = resolve('flarum.formatter')->unparse($latestPost->content);
$currentContent = Arr::get($request->getParsedBody(), 'data.attributes.content', '');
if ($latestContent === $currentContent) {
return true;
}
}
}),
See extend.php.