ソースを参照

Landmark approval setup

Kylie Jo Swistak 5 年 前
コミット
3ca6238cad
5 ファイル変更130 行追加1 行削除
  1. 14 0
      app/controllers/landmark_controller.rb
  2. 68 0
      app/models/landmarks.rb
  3. 2 1
      app/responses/landmark.rb
  4. 45 0
      bot.rb
  5. 1 0
      lib/url.rb

+ 14 - 0
app/controllers/landmark_controller.rb

@@ -0,0 +1,14 @@
+class LandmarkController
+  def self.edit_landmark(params)
+    lm_hash = Landmark.from_form(params)
+
+    if lm = Landmark.find_by(edit_url: lm_hash["edit_url"])
+      lm.update!(lm_hash)
+      lm.reload
+    else
+      lm = Landmark.create(lm_hash)
+    end
+
+    lm
+  end
+end

+ 68 - 0
app/models/landmarks.rb

@@ -2,4 +2,72 @@ class Landmark < ActiveRecord::Base
   validates :name, presence: true
   validates :description, presence: true
   validates :category, presence: true
+
+  def self.from_form(app)
+    key_mapping = {
+      "Description" => "description",
+      "Type" => "category",
+      "Main Region" => "region",
+      "Parent Landmark" => "location",
+      "Rating" => "rating",
+      "Kinks" => "kink",
+      "Warning Description" => "warning",
+      "Warning URL" => "w_url",
+      "Warning Rating" => "w_rating",
+      "History" => "history",
+      "Folklore" => "folk_lore",
+      "Layout URL" => "layout_url"
+    }
+
+    hash = {
+      "name" => nil,
+      "description" => nil,
+      "category" => nil,
+      "url" => nil,
+      "location" => nil,
+      "region" => nil,
+      "warning" => nil,
+      "w_url" => nil,
+      "rating" => nil,
+      "kink" => nil,
+      "layout_url" => nil,
+      "user_id" => nil,
+      "w_rating" => nil,
+      "history" => nil,
+      "folk_lore" => nil,
+      "edit_url" => nil
+    }
+
+    user_id = UID.match(app.description)
+    hash["user_id"] = case app.description
+                      when /server/i
+                        'Server'
+                      else
+                        user_id[1]
+                      end
+
+    hash["name"] = app.title
+    hash["edit_url"] = app.footer.text
+    hash["url"] = app.image&.url
+
+    app.fields.each do |field|
+      next if field.nil?
+
+      db_column = key_mapping[field.name]
+      if db_column == "region"
+        r = Region.find_by(name: field.value)
+        hash[db_column] = r.id
+      elsif db_column == "location"
+        lm = Landmark.find_by(name: field.value)
+        hash[db_column] = lm.id
+      elsif db_column == "kink"
+        hash[db_column] = field.value.split(/,\s?/)
+      else
+        hash[db_column] = field.value
+      end
+    end
+
+    hash = hash.reject { |k,v| k == nil }
+    hash
+  end
 end

+ 2 - 1
app/responses/landmark.rb

@@ -15,6 +15,7 @@ def landmark_embed(lm:, user: nil, section: nil, event: nil)
               end
 
   r = Region.find(lm.region)
+  plm = Landmark.find(lm.location)
   npcs = []
   npc_list = LandmarkNpcs.where(landmark_id: lm.id)
   npc_list.each do |lmnpc|
@@ -36,7 +37,7 @@ def landmark_embed(lm:, user: nil, section: nil, event: nil)
     embed.description = lm.description
     embed.thumbnail = { url: lm.url } if lm.url
 
-    fields.push({name: 'Location', value: lm.location, inline: true}) if lm.location
+    fields.push({name: 'Location', value: plm.name, inline: true}) if lm.location
     fields.push({name: 'Region', value: r.name, inline: true}) if r
     fields.push({name: 'History', value: lm.history}) if lm.history
     fields.push({name: 'Folk Lore', value: lm.folk_lore}) if lm.folk_lore

+ 45 - 0
bot.rb

@@ -1047,6 +1047,10 @@ bot.reaction_add do |event|
       m = event.server.roles.find{ |r| r.id == team_id }.members
       maj = m.count > 2 ? m.count/2.0 : 2
       :team_request
+    when 'Landmark Application'
+      m = event.server.roles.find{ |r| r.id == ENV['ADMINS'].to_i }.members
+      maj = m.count > 2 ? m.count/2.0 : 2
+      :landmark_application
     else
       if event.server == nil
         :new_app
@@ -1182,6 +1186,47 @@ bot.reaction_add do |event|
       self_edit_embed(app, Url::CHARACTER)
     )
 
+  when [:landmark_application, :yes]
+    uid = UID.match(app.description)
+    user =
+      app.description.match(/server/i) ? 'Server' : event.server.member(uid[1])
+    img_url = case
+              when !app.thumbnail&.url.nil? then app.thumbnail.url
+              when !app.image&.url.nil? then app.image.url
+              end
+
+    lm = LandmarkController.edit_landmark(app)
+
+    embed = landmark_embed(lm: lm, user: user, event: event)if lm
+    channel = '453277760429883393'
+    if embed
+      bot.send_message(
+        channel.to_i,
+        "Good news, #{uid}! Your landmark was approved",
+        false,
+        embed
+      )
+      event.message.delete
+    else
+      event.respond(
+        "",
+        admin_error_embed("Something went wrong when saving application")
+      )
+    end
+
+  when [:landmark_application, :cross]
+    event.message.delete
+
+  when [:landmark_application, :crayon]
+    event.message.delete
+    bot.send_temporary_message(
+      event.channel.id,
+      "",
+      35,
+      false,
+      self_edit_embed(app, Url::LANDMARK)
+    )
+
   when [:new_app, :phone]
     event.message.delete_own_reaction(Emoji::PHONE)
     user = event.message.reacted_with(Emoji::PHONE).first

+ 1 - 0
lib/url.rb

@@ -5,4 +5,5 @@ module Url
   CHAR = "https://forms.gle/6HT931F1DvBsHMBf6"
 
   ITEM = "https://docs.google.com/forms/d/e/1FAIpQLSf1wJsagsgGCxO_gj4Ea3pHU7eusSsezmI0CYZexBPEK7dsOw/viewform"
+  LANDMARK = "https://docs.google.com/forms/d/e/1FAIpQLSc1aFBTJxGbvauUOGF1WGEvik5SJ_3SFkyIfbR2h8eK8Fxe7Q/viewform"
 end