Commit Graph

172 Commits

Author SHA1 Message Date
evazion
d791924aad posts: fix /posts/random?tags=... redirect.
Fix /posts/random?tags=touhou to redirect to /posts/1234?q=touhou instead of /posts/1234?tags=touhou.

Fixes the search bar on random posts not containing the current search.
2022-11-15 19:38:43 -06:00
evazion
f942768ce8 Fix #5347: Don't use exception template for post validation errors
Also fixes #5173: Parenting a post to itself using the "parent id" box leads to an error.
2022-11-13 02:01:18 -06:00
evazion
2219a64f47 posts: show takedown page for banned artists and posts.
Show a "This page has been removed because of a takedown request" error when
an unauthorized user searches for a banned tag, or tries to view a banned post.
2022-05-03 05:56:14 -05:00
evazion
5050ca527d posts: eager load uploader and media asset on posts show page.
Eliminate a couple SQL queries by loading the uploader and media asset
in the same query as the post.
2022-04-18 00:18:54 -05:00
evazion
7584578166 posts: fix post query logging. 2022-04-17 23:20:22 -05:00
nonamethanks
1a990d5ab9 Allow post disapprovals to be edited 2022-04-11 21:05:44 +02:00
evazion
6f5aef1cef uploads: fix being redirected to blank page when rating is not selected.
Fix the upload page redirecting you to a blank page if you forgot to
select the rating on a multi-asset upload.

ref: https://danbooru.donmai.us/forum_posts/206365
2022-02-23 02:50:40 -06:00
evazion
bdf83d1ffd uploads: refactor /uploads/:id page for multi-file uploads. 2022-02-14 00:41:08 -06:00
evazion
345a222163 Fix #4999: Unexpected error: ActiveRecord::RecordNotUnique sometimes appears when uploading posts
Fix two issues that could lead to duplicate errors when creating posts:

* Fix the submit button on the upload form to disable itself on submit, to prevent
  accidental double submit errors.

* Fix a race condition when checking for MD5 duplicates. MD5 uniqueness is checked on both
  the Rails level, with a uniqueness validation, and on the database level, with a unique
  index on the md5 column. Creating a post could fail with an ActiveRecord::RecordNotUnique
  error if the uniqueness validation in Rails passed, but the uniqueness constraint in the
  database failed. In this case, we catch the RecordNotUnique error and convert it to a
  Rails validation error so we can treat it like a normal validation failure.
2022-02-07 21:02:30 -06:00
evazion
2b1c58c959 Fix #4987: Can't populate tag string from upload url anymore.
Usage: https://danbooru.donmai.us/uploads/new?url=...&post[tag_string]=...&post[rating]=...

* Pass the URL parameters from the /uploads/new page to the /uploads/:id page.
* Fix the /uploads/:id page throwing an "unpermitted parameters" error
  when given URL params for the post edit form.
2022-02-03 19:41:04 -06:00
evazion
2fe058eccf uploads: fix tag warnings not being shown after upload.
Fix the warnings about not having enough tags, or not having an artist
or copyright tag, not being shown after creating a new post.
2022-01-30 22:39:54 -06:00
evazion
43c4158d36 uploads: merge tags when a duplicate is uploaded (fix #3130).
Automatically merge tags when uploading a duplicate.

There are two cases:

* You try to upload an image, but it's already on Danbooru. In this case
  you'll be immediately redirected to the original post, before you
  can start tagging the upload.

* You're uploading an image, it wasn't a dupe when you first opened the
  upload page, but you got sniped while tagging it. In this case your tags
  will be merged with the original post, and you will be redirected to the
  original post.

There are a few corner cases:

* If you don't have permission to edit the original post, for example
  because it's banned or has a censored tag, then your tags won't be
  merged and will be silently ignored.

* Only the tags, rating, and parent ID will be merged. The source and
  artist commentary won't be merged. This is so that if an artist uploads
  the exact same file to multiple sites, the new source won't override
  the original source.

* Some tags might be contradictory. For example, the new post might
  be tagged translation_request, but the original post might already be
  translated. It's up to the user to fix these things afterwards.
2022-01-30 03:14:22 -06:00
evazion
abdab7a0a8 uploads: rework upload process.
Rework the upload process so that files are saved to Danbooru first
before the user starts tagging the upload.

The main user-visible change is that you have to select the file first
before you can start tagging it. Saving the file first lets us fix a
number of problems:

* We can check for dupes before the user tags the upload.
* We can perform dupe checks and show preview images for users not using the bookmarklet.
* We can show preview images without having to proxy images through Danbooru.
* We can show previews of videos and ugoira files.
* We can reliably show the filesize and resolution of the image.
* We can let the user save files to upload later.
* We can get rid of a lot of spaghetti code related to preprocessing
  uploads. This was the cause of most weird "md5 confirmation doesn't
  match md5" errors.

(Not all of these are implemented yet.)

Internally, uploading is now a two-step process: first we create an upload
object, then we create a post from the upload. This is how it works:

* The user goes to /uploads/new and chooses a file or pastes an URL into
  the file upload component.
* The file upload component calls `POST /uploads` to create an upload.
* `POST /uploads` immediately returns a new upload object in the `pending` state.
* Danbooru starts processing the upload in a background job (downloading,
  resizing, and transferring the image to the image servers).
* The file upload component polls `/uploads/$id.json`, checking the
  upload `status` until it returns `completed` or `error`.
* When the upload status is `completed`, the user is redirected to /uploads/$id.
* On the /uploads/$id page, the user can tag the upload and submit it.
* The upload form calls `POST /posts` to create a new post from the upload.
* The user is redirected to the new post.

This is the data model:

* An upload represents a set of files uploaded to Danbooru by a user.
  Uploaded files don't have to belong to a post. An upload has an
  uploader, a status (pending, processing, completed, or error), a
  source (unless uploading from a file), and a list of media assets
  (image or video files).

* There is a has-and-belongs-to-many relationship between uploads and
  media assets. An upload can have many media assets, and a media asset
  can belong to multiple uploads. Uploads are joined to media assets
  through a upload_media_assets table.

  An upload could potentially have multiple media assets if it's a Pixiv
  or Twitter gallery. This is not yet implemented (at the moment all
  uploads have one media asset).

  A media asset can belong to multiple uploads if multiple people try
  to upload the same file, or if the same user tries to upload the same
  file more than once.

New features:

* On the upload page, you can press Ctrl+V to paste an URL and immediately upload it.
* You can save files for upload later. Your saved files are at /uploads.

Fixes:

* Improved error messages when uploading invalid files, bad URLs, and
  when forgetting the rating.
2022-01-28 04:13:22 -06:00
evazion
1518c3c4be posts: fix search queries not being logged to NewRelic in some cases (#4900)
Fix the /posts index controller not logging the normalized search query
to NewRelic when the search failed, either because of a tag limit error,
a search timeout, or a RSS feed rate limit error.

Also don't log the number of search results when it's an API request or
failed search. This is to avoid doing a potentially slow full post count
when it's not otherwise needed.
2022-01-11 13:39:30 -06:00
evazion
a7dc05ce63 Enable frozen string literals.
Make all string literals immutable by default.
2021-12-14 21:33:27 -06:00
evazion
0997f5595e posts: increase default thumbnail size.
* Increase the default thumbnail size from small (150x150) to medium (180x180).
* Change the mobile layout to use three posts per row instead of two for small thumbnails.

Parent/child posts are still 150x150 to avoid taking up even more space above posts.
2021-12-13 05:23:38 -06:00
evazion
36985e9590 api: add rate limit to /posts.atom endpoint.
Add a rate limit of 1 request per 2 seconds to the post RSS feed
endpoint (/posts.atom).

This lets you check your feeds 30 times per minute, or 1800 times per
hour. The previous limit was 10 requests per second.

This is because there are too many bad RSS feed reader bots constantly
checking the same tags over and over again, 24 hours a day, as fast
they can.
2021-12-10 01:46:01 -06:00
evazion
7976d12cd0 Fix #4930: "Show scores" setting should be preserved
Make "show scores" setting persistent.

The setting is stored in a `post_preview_show_votes` cookie. This means
it's remembered on a per-device basis, but not on a per-account basis.
This is so users without an account can use the setting, and so you can
use different settings on desktop and mobile.

The `view=score` URL param has been replaced by `show_votes=true`. The
`show_votes` URL param overrides the `post_preview_show_votes` cookie.
2021-12-09 15:47:10 -06:00
evazion
3de93f556e posts: make thumbnail size setting persistent.
Make setting the thumbnail size persistent.

The setting is stored in a `post_preview_size` cookie. This cookie can
be overridden by the `size` URL param, like so:

  https://danbooru.donmai.us/posts?tags=touhou&size=180

The `size` param is mainly for testing different sizes without setting a cookie.
2021-12-09 15:46:08 -06:00
evazion
8841de68ac posts: add adjustable thumbnail sizes (#4932).
Add a menu in the top right of the post index page that lets you select
the thumbnail size.

This menu is currently hidden until the new thumbnails have been generated.

On desktop, there are five thumbnail sizes:

* Small:    150x150 (https://danbooru.donmai.us/posts?size=150)
* Medium:   180x180 (https://danbooru.donmai.us/posts?size=180)
* Large:    225x225 (https://danbooru.donmai.us/posts?size=225)
* Huge:     270x270 (https://danbooru.donmai.us/posts?size=270)
* Gigantic: 360x360 (https://danbooru.donmai.us/posts?size=360)

On mobile, there are four sizes:

* Small:  150x150 / 3 posts per row (https://danbooru.donmai.us/posts?size=150)
* Medium: 180x180 / 2 posts per row (https://danbooru.donmai.us/posts?size=180)
* Large:  225x225 / 2 posts per row (https://danbooru.donmai.us/posts?size=225)
* Huge:   360x360 / 1 posts per row (https://danbooru.donmai.us/posts?size=360)

There are two extra sizes that aren't listed in the menu:

* 225x360 (https://danbooru.donmai.us/posts?size=225w)
* 270x360 (https://danbooru.donmai.us/posts?size=270w)

These sizes are good for tall thumbnails, but not so much for wide
thumbnails. They aren't listed because in practice they're a bit too big.

The 225x225 and 270x270 sizes are really just 360x360 thumbnails scaled
down in HTML. This means 225x225 and 360x360 thumbnails both use the
same amount of bandwidth.

Thumbnail size is currently a per-search option, not a persistent
account-level setting.

This changes the HTML structure of thumbnails somewhat, so this may
break userscripts and custom CSS.
2021-12-04 06:30:29 -06:00
evazion
0baca68a37 search: make order:random truly random; add random:N metatag.
Make the `order:random` metatag truly randomize the search. Add a
`random:N` metatag that returns up to N random posts, like what
`order:random` did before.

`order:random` now returns the entire search in random order. Before it
just returned a pageful of pseudorandom posts. This will be more
accurate for small searches, but slower for large searches. If
`order:random` times out, try `random:N` instead.

The `random:N` metatag returns up to N pseudorandom posts. This is
faster than `order:random` for large searches, but for small searches,
it may return less than N posts, and the randomness may be biased. Some
posts may be more likely than others to appear. N must be between 0 and
200.

Also, `/posts?tags=touhou&random=1` now redirects to `/posts?tags=touhou+random:N`.
Before the `random=1` param acted like a free `order:random` tag; now it
redirects to a `random:N` search, so it counts against your tag limit.
2021-11-25 18:14:34 -06:00
evazion
bd8672681f votes: add vote buttons beneath thumbnails.
Add upvote and downvote buttons beneath thumbnails on the post index page.

This is disabled by default. To enable it, click the "..." menu in the top
right of the page, then click "Show scores".

This is currently a per-search setting, not an account setting. If you
enable it in one tab, it won't be enabled in other tabs.
2021-11-18 04:15:20 -06:00
evazion
f1b5c34b4d posts: show length of videos and animations in thumbnails.
Show the length of videos and animated posts in the thumbnail. The
length is shown the top left corner in MM:SS format. This replaces the
play button icon.

Show a speaker icon instead of a music note icon for posts with sound.

Doing this requires doing `.includes(:media_asset)` in a bunch of
places to avoid N+1 queries when we access the post's duration.
2021-10-25 02:56:55 -05:00
evazion
79fdfa86ae Fix various rubocop warnings. 2021-09-27 00:46:13 -05:00
evazion
c29539d44e /posts/random: fix reorder(nil).first deprecation warning
DEPRECATION WARNING: `.reorder(nil)` with `.first` / `.first!` no
    longer takes non-deterministic result in Rails 6.2. To continue
    taking non-deterministic result, use `.take` / `.take!` instead.
    (called from random at /home/user/src/danbooru/app/controllers/posts_controller.rb:91)
2021-09-02 04:09:03 -05:00
evazion
07e23204b6 rubocop: fix various Rubocop warnings. 2021-06-17 04:17:53 -05:00
evazion
698be2d0e4 discord: add /random command. 2021-03-11 21:23:20 -06:00
evazion
07bdc6eab0 comments: rework thresholded comments.
Previously thresholded comments were hidden completely. You had to click
the "Show X hidden comments" button to unhide all hidden comments in a
thread. Now it works like this:

* When a comment is below your threshold, the comment text is hidden and
  replaced by a `[hidden]` link, which you can click to unhide the comment.

* When a comment is at half your threshold (for example, your threshold
  is -8 but the comment is at -4), then the comment is greyed out.

This means that comments aren't completely hidden, they're just
collapsed, so you can see the commenter and the score without unhiding
the comment. It also means you don't have to scroll back up to unhide a
comment, and threads aren't disrupted by comments being secretly
hidden (which is confusing when people are replying to hidden comments,
which forces you to go back up and unhide to find).
2021-01-19 04:07:33 -06:00
evazion
81708d2ef8 search: log extra search metadata to NewRelic.
When a user does a tag search, log a few more things, including the normalized
search string, the number of tags in the search string, and the number of results.
2021-01-12 04:25:11 -06:00
evazion
5bad5c6012 comments: fix visible method conflict.
Fix the `Comment#visible` method conflicting with the base class
`visible` method defined in ApplicationRecord.
2020-08-18 15:34:12 -05:00
evazion
157cb96551 posts: clean up delete! method.
* Remove unused `ban` and `without_mod_action` options.

* Don't try to set the `is_banned` flag during deletion.

* Don't create modactions for automatic "unapproved in 3 days"
  deletions, only to delete them after the fact.
2020-08-03 20:21:28 -05:00
evazion
bca1f122d0 posts: rework post deletion to use dialog box.
Rework post deletion from using a separate page to using a dialog box,
like flagging.

* Add `DELETE /posts/:id` endpoint.
* Remove `POST /moderator/post/posts/:id/delete` endpoint.
2020-08-03 20:21:28 -05:00
evazion
f38c38f26e search: split tag_match into user_tag_match / system_tag_match.
When doing a tag search, we have to be careful about which user we're
running the search as because the results depend on the current user.
Specifically, things like private favorites, private favorite groups,
post votes, saved searches, and flagger names depend on the user's
permissions, and whether non-safe or deleted posts are filtered out
depend on whether the user has safe mode on or the hide deleted posts
setting enabled.

* Refactor internal searches to explicitly state whether they're
  running as the system user (DanbooruBot) or as the current user.
* Explicitly pass in the current user to PostQueryBuilder instead of
  implicitly relying on the CurrentUser global.
* Get rid of CurrentUser.admin_mode? (used to ignore the hide deleted
  post setting) and CurrentUser.without_safe_mode (used to ignore safe
  mode).
* Change the /counts/posts.json endpoint to ignore safe mode and the
  hide deleted posts settings when counting posts.
* Fix searches not correctly overriding the hide deleted posts setting
  when multiple status: metatags were used (e.g. `status:banned status:active`)
* Fix fast_count not respecting the hide deleted posts setting when the
  status:banned metatag was used.
2020-05-07 03:29:44 -05:00
evazion
2cbe4d3672 search: add unaliased:<tag> metatag.
* Add unaliased:<tag> metatag. This allows you to search for a tag
  without applying aliases. This is mainly useful for debugging purposes
  and for searching for large tags that are in the process of being
  aliased but haven't had all their posts moved yet.

* Remove the "raw" url param from the posts index page. The "raw" param
  also caused the search to ignore aliases, but it was undocumented and
  exploitable. It was possible to use the raw param to view private
  favorites since favorites are treated like a hidden tag.
2020-04-30 17:45:07 -05:00
evazion
25776a062c posts/random: fix pundit exception when no post is found. 2020-03-31 21:57:34 -05:00
evazion
d24c746417 Fix #4360: Something broke random=true.
When random mode is enabled @post_set.posts returns an array, which
caused `authorize` to try to lookup the wrong policy.

This only happens when `authorize` is given an array with more than one
element, which is why it wasn't caught by the tests.
2020-03-27 04:23:32 -05:00
evazion
a5418abb31 pundit: convert posts to pundit. 2020-03-20 18:03:01 -05:00
evazion
869142ed1b Fix #4314: Favorite/vote modes give generic error messages.
Refactor tag scripts to fix multiple issues:

* Errors during tag scripting didn't show the actual error message, just
  a generic "There was an error updating post #NNN" message.
* The quick edit form didn't show any error messages at all on failure.
* Thumbnails didn't have all their data attributes properly updated
  after the post was updated.

This changes it so that thumbnails have their html fully replaced after
updating. This has the side effect of removing event handlers bound
directly to the thumbnail. A `danbooru:post-preview-updated` event is
fired in case userscripts need to detect when thumbnails are updated.
2020-02-27 19:23:48 -06:00
evazion
caf54fe45a blacklists: remove ability for mods to blacklist uploaders by name. 2020-02-16 04:00:42 -06:00
evazion
2564e885c8 controllers: refactor only param includes.
Add extra includes needed by the `only` param inside `respond_with`.
2020-02-15 06:17:22 -06:00
evazion
8649ff6dbe API: remove various associated fields included by default.
Remove various associated fields that were included by default on
certain endpoints. API users can use the only param to include the
full association if they need these fields.

* /artists.json: urls.
* /artist_urls.json: artist.
* /comments.json: creator_name and updater_name.
* /notes.json: creator_name.
* /pools.json: creator_name.
* /posts.json: uploader_name, children_ids, pixiv_ugoira_frame_data.
* /post_appeals.json: is_resolved.
* /post_versions.json: updater_name.
* /uploads.json: uploader_name.
2020-02-15 06:17:11 -06:00
evazion
1e426a50f7 controllers: standardize request format checks. 2020-02-14 17:28:25 -06:00
BrokenEagle
75f4af8991 Don't do unnecessary processing on posts when using the API 2020-02-12 23:58:54 +00:00
BrokenEagle
63b3503bfc Add ability to use nested only parameter
- The only string works much the same as before with its comma separation
-- Nested includes are indicated with square brackets "[ ]"
-- The nested include is the value immediately preceding the square brackets
-- The only string is the comma separated string inside those brackets
- Default includes are split between format types when necessary
-- This prevents unnecessary includes from being added on page load
- Available includes are those items which are allowed to be accessible to the user
-- Some aren't because they are sensitive, such as the creator of a flag
-- Some aren't because the number of associated items is too large
- The amount of times the same model can be included to prevent recursions
-- One exception is the root model may include the same model once
--- e.g. the user model can include the inviter which is also the user model
-- Another exception is if the include is a has_many association
--- e.g. artist urls can include the artist, and then artist urls again
2020-02-12 23:58:53 +00:00
evazion
c7185724d5 controllers: set @current_item globally in respond_with. 2020-01-31 02:43:08 -06:00
evazion
40711e1d4f posts/show: refactor parent/child notices.
* Convert notices from helpers to partials.
* Eliminate PostSets::PostRelationship class in favor of post_sets/posts template.
* Eliminate COUNT(*) queries when calculating the number of child posts.
* Eliminate redundant parent load and parent exists queries.
2020-01-12 18:38:38 -06:00
BrokenEagle
223a6df5d8 Add API data to show/index views 2020-01-04 22:02:44 +00:00
evazion
309821bf73 rubocop: fix various style issues. 2019-12-22 21:23:37 -06:00
evazion
960e5d4ae0 views: factor out sidebar layout template.
* Factor out common sidebar layout template.
* Convert wiki pages and posts to use this template.
* Add data-layout attribute to <body> element indicating the current layout.
2019-09-28 17:50:10 -05:00
evazion
3f7e05316d api: refactor default options for xml responses.
In xml responses, if the result is an empty array we want the response
to look like this:

   <posts type="array"/>

not like this (the default):

   <nil-classes type="array"/>

This refactors controllers so that this is done automatically instead of
having to manually call `@things.to_xml(root: "things")` everywhere. We
do this by overriding the behavior of `respond_with` in `ApplicationResponder`
to set the `root` option by default in xml responses.
2019-09-08 15:32:31 -05:00