about summary refs log tree commit diff
path: root/app/lib/bangtags.rb
diff options
context:
space:
mode:
authormultiple creatures <dev@multiple-creature.party>2019-09-30 23:28:52 -0500
committermultiple creatures <dev@multiple-creature.party>2019-10-01 00:37:34 -0500
commitee1448ab1f4cc640e60515b6214c6fde1d98d4cf (patch)
treed3fc06f261607c883b4a95630be2b07035d338ab /app/lib/bangtags.rb
parent4a8e63023248f71e3dba13de7b39182c826387a8 (diff)
add `histogram` bangtag
Diffstat (limited to 'app/lib/bangtags.rb')
-rw-r--r--app/lib/bangtags.rb27
1 files changed, 27 insertions, 0 deletions
diff --git a/app/lib/bangtags.rb b/app/lib/bangtags.rb
index 31395740d..81aef2807 100644
--- a/app/lib/bangtags.rb
+++ b/app/lib/bangtags.rb
@@ -700,6 +700,33 @@ class Bangtags
           next unless @sroff_open
           @sroff_open = false
           chunk = "\uf334"
+        when 'histogram'
+          @status.content_type = 'text/html'
+          barchars = " #{(0x2588..0x258F).to_a.reverse.pack('U*')}"
+          q = cmd[1..-1].join.strip
+          if q.start_with?('@@')
+            sql = 'tsv @@ to_tsquery(?)'
+            q = q[2..-1].lstrip
+          else
+            sql = 'tsv @@ plainto_tsquery(?)'
+          end
+          next if q.blank?
+          begin
+            data = @account.statuses.where(sql, q)
+              .reorder(:created_at)
+              .pluck(:created_at)
+              .map { |d| d.strftime('%Y-%m') }
+              .reduce(Hash.new(0)) { |h, v| h.store(v, h[v] + 1); h }
+          rescue ActiveRecord::StatementInvalid
+            raise Mastodon::ValidationError, 'Your advanced search query has invalid syntax.'
+          end
+          highest = data.values.max
+          data = data.map do |date, count|
+            fill = count / highest.to_f * 96
+            bar = "#{"\u2588" * (fill / 8).to_i}#{barchars[fill % 8]}"
+            "<code>#{date}: #{bar} #{count}</code>"
+          end
+          chunk = "\"<code>#{q.split('').join("\u200c")}</code>\" mentions by post count:<hr/>#{data.join("<br/>")}"
         when 'admin'
           chunk = nil
           next unless @user.admin?