Commit Graph

9914 Commits

Author SHA1 Message Date
evazion
2c1da660fd tags: allow tag abbreviations in searches and during tagging.
Expand the tag abbreviation system introduced in b0be8ae45 so that it
works in searches and when tagging posts, not just in autocomplete.

For example, you can tag a post with /evth and it will add the tag
eyebrows_visible_through_hair. You can search for /evth and it will
search for the tag eyebrows_visible_through_hair.

Some more examples:

* /ops is short for one-piece_swimsuit
* /hooe is short for hair_over_one_eye
* /saol is short for standing_on_one_leg
* /tlozbotw is short for the_legend_of_zelda:_breath_of_the_wild

If two tags have the same abbreviation, then the larger tag takes
precedence. For example, /be is short for blue_eyes, not brown_eyes,
because blue_eyes is the bigger tag.

If there is an existing shortcut alias that conflicts with the
abbreviation, then the alias take precedence. For example, /sh is short
for suzumiya_haruhi, not short_hair, because there's an old alias for
/sh -> suzumiya_haruhi.
2020-12-17 23:57:13 -06:00
evazion
991896c4eb tags: don't allow tags more than 170 chars long.
Limit tag length to 170 chars. 170 chars was chosen because it's
longer than the longest active tag on Danbooru.

Tag length is limited because in some contexts we can't deal with
excessively long tags. Tag autocorrect for example uses the levenshtein
function in Postgres, which can't handle strings more than 255 chars long.
2020-12-17 21:38:24 -06:00
evazion
1809f67b2b tags: don't allow tags to begin with a '/'.
Disallow tags from starting with a '/' character. This is so that tag
abbreviations in autocomplete, which start with a '/', don't conflict
with regular tags.

Also disallow some other punctuation characters: `%{})]. Currently no
tags start with these characters. This is to reserve other special
characters in case we need them for other future syntax extensions.
2020-12-17 21:38:18 -06:00
evazion
7a87225ac8 Add basic server status page at /status.
Lists versions of various dependencies plus some Postgres and Redis
metrics.
2020-12-17 03:13:54 -06:00
evazion
ee4516f5fe searchable: refactor searchable_includes.
Pass searchable associations directly to search_attributes instead of
defining them separately in searchable_includes.
2020-12-16 23:57:07 -06:00
evazion
e771c0fca8 searchable: don't automatically include id, created_at, updated_at.
Don't make search methods on models call super in order to search
certain default attributes (id, created_at, updated_at). Simplifies some
magic.
2020-12-16 23:57:07 -06:00
evazion
b0659eb76c searchable: add tests for Searchable concern. 2020-12-16 23:57:04 -06:00
evazion
2297bf5da5 Fix #4638: Add exclusions to the numeric attributes.
Add the following search operators:

* /tags?search[post_count_eq]=42
* /tags?search[post_count_not_eq]=42
* /tags?search[post_count_gt]=42
* /tags?search[post_count_gteq]=42
* /tags?search[post_count_lt]=42
* /tags?search[post_count_lteq]=42

Works for all numeric attributes on all index actions.
2020-12-16 20:03:09 -06:00
evazion
0d83106a21 autocomplete: fix cache issue related to content negotiation.
This is the scenario:

* You type something in autocomplete, let's say 'touhou'.
* Autocomplete calls /autocomplete?search[query]=touhou&search[type]=tag_query
* The endpoint returns JSON, because the autocomplete call sets an
  `Accept: application/json` header requesting JSON.
* Visit /autocomplete?search[query]=touhou&search[type]=tag_query in your browser.
* Notice that the cached JSON response is incorrectly returned, not an
  HTML response like the browser requested.

The problem is that the response type is chosen based on the Accept
header, but the response didn't set the `Vary: Accept` header, so the
browser doesn't know the response type can vary and so it incorrectly
returns the cached response.

This issue is partially fixed by Rails 6.1 ([1]), which properly sets the
`Vary: Accept` header when the response depends on the Accept header.

However, the next issue is that Cloudflare doesn't respect the Vary
header at all ([2], [3]). Therefore we can't use the Accept header to
pick the format, instead we have explicitly specify the format with
/autocomplete.json.

This is clearer and better for caching anyway. Using the `Vary: Accept`
header reduces the cache hit rate, because the exact format of the
Accept header varies across browsers, which fragments the cache.

Whew.

[1] https://github.com/rails/rails/pull.36213
[2] https://community.cloudflare.com/t/cloudflare-cdn-cache-to-support-http-vary-header/160802
[3] https://support.cloudflare.com/hc/en-us/articles/115003206852
[4] https://www.smashingmagazine.com/2017/11/understanding-vary-header/
2020-12-16 03:13:55 -06:00
evazion
c836c93b81 autocomplete: don't send cookies in publicly cached responses.
Fix session cookies being sent in publicly cached /autocomplete.json
responses. We can't set any cookies in a response that is being publicly
cached, otherwise they'll be visible to other users. If a user's session
cookies were to be cached, then it would allow their account to be stolen.

In reality, well-behaved caches like Cloudflare will simply refuse to
cache responses that contain cookies to avoid this scenario.

https://support.cloudflare.com/hc/en-us/articles/200172516-Understanding-Cloudflare-s-CDN:

    BYPASS is returned when enabling Origin Cache-Control. Cloudflare also
    sets BYPASS when your origin web server sends cookies in the response
    header.
2020-12-15 03:48:59 -06:00
evazion
26246b0ac9 autocomplete: fix exception when typing "/" in autocomplete.
Fix an exception that could occur when typing "/" by itself in
autocomplete and a regular tag starting with "/" was returned. This
caused an exception in `r[:antecedent].length` because the tag's
antecedent was nil.
2020-12-14 21:57:28 -06:00
evazion
4cdaf7bcdf autocomplete: update html data attributes.
* Remove the `source` and `weight` html data attributes (no longer used).
* Make the `type` html data attribute properly indicate the completion
  type. Valid types: `tag`, `tag-alias`, `tag-abbreviation`,
  `tag-autocorrect`, `tag-other-name`.
2020-12-14 18:58:11 -06:00
evazion
c02c31b966 autocomplete: recognize Japanese tags in autocomplete.
Allowing typing Japanese tags in autocomplete. For example, typing 東方
in autocomplete will be completed to the touhou tag. Typing ぶくぶ will
complete to the bkub tag.

This works using wiki page and artist other names. Effectively, any name
listed as an other name in a wiki or artist page will be treated like an
alias for autocomplete purposes. This is limited to non-ASCII other names,
to prevent English other names from interfering with regular tag searches.
2020-12-14 18:58:11 -06:00
evazion
23f6b8a46d js: refactor Cookie.put.
* Set Max-Age= flag instead of Expires= flag.
* Set Secure flag when using HTTPS.
* Extend default cookie lifetime from 1 year to 20 years.
* Remove "session" expiration option (unused).
* Remove max cookie size check.

The cookie size check was previously added in #2518 to deal with running
out of space due to tag scripts and blacklists. This should no longer
happen since we no longer use cookies for these things. Remove the
warning because it should never happen, we can't fix it if it does, and
the user probably won't know how to fix it either.
2020-12-14 15:39:17 -06:00
evazion
df1404b673 js: set SameSite=Lax on cookies set by Javascript.
This is the new default for most browsers nowadays. Fixes a warning in
Firefox about using SameSite=None without the Secure flag.
2020-12-14 15:37:58 -06:00
evazion
0150911343 css: remove missing --dtext-expand-border-color var. 2020-12-14 14:29:31 -06:00
evazion
852c67f1b7 users: fix #owner method. 2020-12-14 14:13:11 -06:00
evazion
23ee39010a Update ruby gems and yarn packages. 2020-12-14 03:00:43 -06:00
evazion
eae3c1942d dmails: allow site owner to read all mails.
Allow site owner to read dmails sent to other users. This is make it
easier to investigate spam without having to drop into the dev console.
2020-12-13 23:59:53 -06:00
evazion
2e633f84f6 emails: add /emails index page.
Add emails index page at https://danbooru.donmai.us/emails. Mods can use
this page to view and search emails belonging to users below mod level.
2020-12-13 21:21:08 -06:00
evazion
67eefadd7f users: let mods see email addresses on user profiles.
* Let Mods and Admins see the email addresses of users below their level.
* Let users see their own email address on their profile.
* Let users verify or edit their email address from their profile.

This is to make catching sockpuppets easier, and to make it easier for
users to fix their email.
2020-12-13 21:21:08 -06:00
evazion
9f09c495e4 users: don't allow admins to edit user levels directly.
Don't allow admins to bypass promotion restrictions by manually updating
user levels with a `PUT /users/:id` API call. Level changes have to go
through the /admin/users/:id/edit page.
2020-12-13 21:21:08 -06:00
evazion
86bba56eda users: allow site owner to reset passwords of other users. 2020-12-13 21:21:08 -06:00
evazion
d8b51e3f02 users: don't allow gifting upgrades to demote privileged users.
Don't allow gifting Gold or Platinum upgrades to users above Platinum
level. Fixes an exploit where you could demote Builders and above by
gifting them an upgrade.
2020-12-13 21:21:08 -06:00
evazion
2144f45fa4 users: add account upgrade integration tests.
* Test that the user upgrade process integrates with Stripe correctly.
* Replace a deprecated `card` param with `source` in `Stripe::Charge.create`.
* Rescue Stripe::StripeError instead of Stripe::CardError so that we
  handle failures outside of card failures, such as network errors.
2020-12-13 21:21:08 -06:00
evazion
c82e05d828 users: add stricter checks for user promotions.
New rules for user promotions:

* Moderators can no longer promote other users to moderator level. Only
  Admins can promote users to Mod level. Mods can only promote up to Builder level.
* Admins can no longer promote other users to Admin level. Only Owners
  can promote users to Admin. Admins can only promote up to Mod level.
* Admins can no longer demote themselves or other admins.

These rules are being changed to account for the new Owner user level.

Also change it so that when a user upgrades their account, the promotion
is done by DanbooruBot. This means that the inviter and the mod action
will show DanbooruBot as the promoter instead of the user themselves.
2020-12-13 21:21:08 -06:00
evazion
b3ad13e6e3 users: add new owner level.
Add a new Owner user level for the site owner. Highly sensitive
operations like manually changing the passwords of other users will be
restricted to the site owner.
2020-12-13 21:18:24 -06:00
evazion
35134abe8f post query builder: fix incompatibilities with Rails 6.1.
* Rename the `#negate` and `#and` methods that we monkey patch into
  ActiveRecord::Relation. These methods are now defined in Rails 6.1, but
  they shadow our methods and have slightly different behavior.
* Fix a call to `invert`. It no longer accepts an argument.
2020-12-13 04:10:48 -06:00
evazion
8d87b1a0c0 models: fix deprecated errors[:base] << "message" calls.
Replace the idiom `errors[:base] << "message"` with
`errors.add(:base, "message")`. The former is deprecated in Rails 6.1.
2020-12-13 04:10:48 -06:00
evazion
62b69eb133 gems: upgrade http-cookie to fix Rails 6.1 bug.
Upgrade the http-cookie gem to a personal fork containing a bugfix for a
http-cookie bug that is triggered by Rails 6.1.

The bug is that HTTP::Cookie objects raise an exception if they're
compared against non-cookie objects. This bug gets triggered when
the Nijie source strategy calls `Rails.cache.fetch` to cache the
Nijie login cookie. `Rails.cache.fetch` ends up calling
ActiveSupport::Cache::Store::Entry#dup_value!, which compares the cookie
with `true`, which triggers the exception.

The http-cookie gem hasn't been updated for 4 years, so we're stuck
patching the library ourselves.
2020-12-13 04:10:48 -06:00
evazion
8f1d8e2c56 mailers: fix Rails 6.1 incompatibility.
`add_template_helper` is removed in Rails 6.1.
2020-12-13 04:10:48 -06:00
evazion
61e7d32f78 tests: fix FC2 artist normalization url test. 2020-12-13 04:10:48 -06:00
evazion
71ba45c57c forum: fix /forum_posts?search[linked_to] not normalizing tags.
Fix searches like https://danbooru.donmai.us/forum_posts?search[linked_to]=touhou%20
not working because the tag wasn't normalized.
2020-12-13 04:10:48 -06:00
evazion
b002bf25f5 autocomplete: display autocorrected tags like aliases.
Display autocorrected tags similar to aliases, with an arrow pointing at
the corrected tag, but with a dotted underline beneath the misspelled
tag to indicate that it's misspelled.
2020-12-13 04:10:48 -06:00
evazion
6a46aeb55c autocomplete: tune autocorrect algorithm.
Tune autocorrect to produce fewer false positives. Before we used
trigram similarity. Now we use Levenshtein edit distance with a dynamic
typo threshold. Trigram similarity was able to correct large
transpositions (e.g. `miku_hatsune` -> `hatsune_miku`), but it was bad
at correcting small typos. Levenshtein is good at small typos, but can't
correct large transpositions.
2020-12-13 04:10:48 -06:00
evazion
119268e118 autocomplete: fix exception when completing saved search labels.
Fix an exception that was thrown when trying to autocomplete saved
search labels (e.g. `search:all`) as an anonymous user. This was a
pre-existing bug.
2020-12-13 00:45:22 -06:00
evazion
d6a5b9e252 autocomplete: rework cache policy.
The previous cache policy was that all autocomplete results were cached
for a fixed 7 days. The new policy is that if autocomplete returns more
than 10 results they're cached for 24 hours, otherwise if it returns
less than 10 results they're cached for 1 hour.

The rationale is that if autocomplete returns a lot of results, then the
top 10 results are relatively stable and unlikely to change, but if it
returns less than 10 results, then the results are unstable and can be
easily changed.

We also change it so that autocomplete calls can be cached publicly.
Public caching means that HTTP requests are cached by Cloudflare. This
will ideally reduce load on the server and reduce latency for end users.
This is only safe for calls that return the same results for all users
(i.e. the results don't depend on the current user), since the cache is
publicly shared by all users. Currently username, favgroup, and saved
search autocomplete results depend on the current user, so they can't be
publicly cached.
2020-12-13 00:45:22 -06:00
evazion
b0be8ae456 autocomplete: rework tag autocomplete behavior.
Reworks tag autocomplete to work the same way for all users. Previously
autocomplete for Builders worked differently than autocomplete for
regular users.

This is how it works now:

* If the search starts with a slash (/), then do a tag abbreviation
  match. For example, `/evth` matches eyebrows_visible_through_hair.
* Otherwise if the search contains a wildcard (*), then just do a simple
  wildcard search.
* Otherwise do a tag prefix match against tags and aliases. For example,
  `black` matches all tags or aliases beginning with `black`.
* If the tag prefix match returns no results, then do a autocorrect match.

The differences for regular users:

* You can abbreviate tags with a slash (/).

The differences for Builders:

* Now tag abbreviations have to start with a slash (/).
* Autocorrect isn't performed unless a regular search returns no results.
* Results are always sorted by tag count. Before different types of
  results (regular tag matches, alias matches, abbreviation matches,
  and autocorrect matches) were all mixed together based on a tag
  weighting scheme.
2020-12-13 00:45:22 -06:00
evazion
adc1c2c2cc autocomplete: refactor javascript to use /autocomplete endpoint.
This refactors the autocomplete Javascript to use a single dedicated
/autocomplete.json endpoint instead of a bunch of separate endpoints.

This simplifies the autocomplete Javascript by making it so that instead
of calling a different endpoint for each type of query (for users, wiki
pages, pools, artists, etc), then having to parse the results of each
call to get the data we need, we can call a single endpoint that returns
exactly what we need.

This also means we don't have to parse searches clientside in order to
autocomplete metatags. Instead we can just pass the search term to the
server and let it parse the search, which is easy to do serverside.

Finally, this makes autocomplete easier to test, and it makes it easier
to add more sophisticated autocomplete behavior, since most of the logic
lives serverside.
2020-12-13 00:45:22 -06:00
evazion
1484f8852c posts: remove "repopulated 1 old tag" message.
Remove the "Repopulated 1 old tag" message. Show "Created 1 new tag"
instead. The distinction between creating a brand new tag and
repopulating an empty tag doesn't matter.
2020-12-13 00:45:22 -06:00
evazion
be69778d25 BURs: fix validation error when aliasing tags with implications.
Bug: when aliasing a tag that implied another tag, it was possible for
the alias to fail. Moving the implication could fail because we checked
that the tag category of both tags in the implication was the same, but
we did this before the alias moved the category of the old tag to the
new tag.
2020-12-05 15:05:06 -06:00
evazion
9b48c98c61 Fix #4614: Counts endpoint responds with invalid JSON.
Caused by the search timing out and returning nil for the count. Nil got
serialized as the empty string instead of as null.
2020-12-05 13:10:11 -06:00
evazion
cc781ba2b9 tests: add tests for #4551, #4630. 2020-12-05 12:54:32 -06:00
evazion
92b6204a77 Merge pull request #4630 from nonamethanks/fix_fc2
Fix blog.fc2 urls matching wrong artists
2020-12-05 12:53:51 -06:00
evazion
b358714790 tests: add test for #4613. 2020-12-05 12:48:45 -06:00
evazion
d5d77e506f Merge pull request #4631 from nonamethanks/fix_ordfav
Return empty search for ordfav of fake user
2020-12-05 12:46:26 -06:00
evazion
c8a9015e8e Merge pull request #4611 from aaronfranke/formatting
Make file formatting comply with POSIX standards and remove trailing space characters
2020-12-05 12:45:38 -06:00
evazion
ab8d000b8b logins: redirect back to previous page after login. 2020-12-05 12:40:48 -06:00
evazion
c42f4fae9d BURs: allow implying empty tags.
Allow implications when either tag is empty to account for things like
aliases or mass updates moving tags around.
2020-12-05 12:32:28 -06:00
evazion
7f9fbc25b9 dtext: fix username mentions in [bur:id] tags.
Use the regular name (with underscores) instead of the pretty name (with
spaces) because <@mention> syntax can't handle spaces.
2020-12-05 00:55:09 -06:00