REFACTOR: No need for `return` at end of each method.

This commit is contained in:
Guo Xiang Tan 2017-10-10 12:02:27 +08:00
parent efddfe1d9b
commit 5d11f443c0
1 changed files with 77 additions and 68 deletions

View File

@ -7,24 +7,20 @@ module DiscourseChat::Provider::SlackProvider
def username
if user
return user['name']
user['name']
elsif @raw.key?("username")
return @raw["username"]
else
return nil
@raw["username"]
end
end
def avatar
return nil unless user
return user["profile"]["image_24"]
user["profile"]["image_24"] if user
end
def url
channel_id = @transcript.channel_id
ts = @raw['ts'].gsub('.', '')
return "https://slack.com/archives/#{channel_id}/p#{ts}"
"https://slack.com/archives/#{channel_id}/p#{ts}"
end
def text
@ -50,7 +46,7 @@ module DiscourseChat::Provider::SlackProvider
"*#{match}*"
end
return text
text
end
def raw_text
@ -67,7 +63,7 @@ module DiscourseChat::Provider::SlackProvider
attachments << attachment["fallback"]
end
return attachments
attachments
end
def ts
@ -75,12 +71,11 @@ module DiscourseChat::Provider::SlackProvider
end
private
def user
return nil unless user_id = @raw["user"]
users = @transcript.users
user = users.find { |u| u["id"] == user_id }
@transcript.users.find { |u| u["id"] == user_id }
end
end
class SlackTranscript
@ -136,26 +131,26 @@ module DiscourseChat::Provider::SlackProvider
if new_first_message_index
@first_message_index = new_first_message_index
return true
true
else
return false
false
end
end
def first_message
return @messages[@first_message_index]
@messages[@first_message_index]
end
def last_message
return @messages[@last_message_index]
@messages[@last_message_index]
end
# These two methods convert potentially negative array indices into positive ones
def first_message_number
return @first_message_index < 0 ? @messages.length + @first_message_index : @first_message_index
@first_message_index < 0 ? @messages.length + @first_message_index : @first_message_index
end
def last_message_number
return @last_message_index < 0 ? @messages.length + @last_message_index : @last_message_index
@last_message_index < 0 ? @messages.length + @last_message_index : @last_message_index
end
def build_transcript
@ -197,7 +192,7 @@ module DiscourseChat::Provider::SlackProvider
post_content << "[#{username}]: #{url}\n"
end
return post_content
post_content
end
def build_slack_ui
@ -205,51 +200,67 @@ module DiscourseChat::Provider::SlackProvider
secret = DiscourseChat::Helper.save_transcript(post_content)
link = "#{Discourse.base_url}/chat-transcript/#{secret}"
return { text: "<#{link}|#{I18n.t("chat_integration.provider.slack.transcript.post_to_discourse")}>",
attachments: [
{
pretext: I18n.t("chat_integration.provider.slack.transcript.first_message_pretext", n: @messages.length - first_message_number),
fallback: "#{first_message.username} - #{first_message.raw_text}",
color: "#007AB8",
author_name: first_message.username,
author_icon: first_message.avatar,
text: first_message.raw_text,
footer: I18n.t("chat_integration.provider.slack.transcript.posted_in", name: @channel_name),
ts: first_message.ts,
callback_id: last_message.ts,
actions: [
{
name: "first_message",
text: I18n.t("chat_integration.provider.slack.transcript.change_first_message"),
type: "select",
options: first_message_options = @messages[ [(first_message_number - 20), 0].max .. last_message_number]
.map { |m| { text: "#{m.username}: #{m.text}", value: m.ts } }
}
],
},
{
pretext: I18n.t("chat_integration.provider.slack.transcript.last_message_pretext", n: @messages.length - last_message_number),
fallback: "#{last_message.username} - #{last_message.raw_text}",
color: "#007AB8",
author_name: last_message.username,
author_icon: last_message.avatar,
text: last_message.raw_text,
footer: I18n.t("chat_integration.provider.slack.transcript.posted_in", name: @channel_name),
ts: last_message.ts,
callback_id: first_message.ts,
actions: [
{
name: "last_message",
text: I18n.t("chat_integration.provider.slack.transcript.change_last_message"),
type: "select",
options: @messages[first_message_number..(last_message_number + 20)]
.map { |m| { text: "#{m.username}: #{m.text}", value: m.ts } }
}
],
}
]
}
{
text: "<#{link}|#{I18n.t("chat_integration.provider.slack.transcript.post_to_discourse")}>",
attachments: [
{
pretext: I18n.t(
"chat_integration.provider.slack.transcript.first_message_pretext",
n: @messages.length - first_message_number
),
fallback: "#{first_message.username} - #{first_message.raw_text}",
color: "#007AB8",
author_name: first_message.username,
author_icon: first_message.avatar,
text: first_message.raw_text,
footer: I18n.t(
"chat_integration.provider.slack.transcript.posted_in",
name: @channel_name
),
ts: first_message.ts,
callback_id: last_message.ts,
actions: [
{
name: "first_message",
text: I18n.t(
"chat_integration.provider.slack.transcript.change_first_message"
),
type: "select",
options: first_message_options = @messages[ [(first_message_number - 20), 0].max .. last_message_number]
.map { |m| { text: "#{m.username}: #{m.text}", value: m.ts } }
}
],
},
{
pretext: I18n.t(
"chat_integration.provider.slack.transcript.last_message_pretext",
n: @messages.length - last_message_number
),
fallback: "#{last_message.username} - #{last_message.raw_text}",
color: "#007AB8",
author_name: last_message.username,
author_icon: last_message.avatar,
text: last_message.raw_text,
footer: I18n.t(
"chat_integration.provider.slack.transcript.posted_in",
name: @channel_name
),
ts: last_message.ts,
callback_id: first_message.ts,
actions: [
{
name: "last_message",
text: I18n.t(
"chat_integration.provider.slack.transcript.change_last_message"
),
type: "select",
options: @messages[first_message_number..(last_message_number + 20)]
.map { |m| { text: "#{m.username}: #{m.text}", value: m.ts } }
}
],
}
]
}
end
def load_user_data
@ -262,9 +273,7 @@ module DiscourseChat::Provider::SlackProvider
return false unless response.kind_of? Net::HTTPSuccess
json = JSON.parse(response.body)
return false unless json['ok']
@users = json['members']
json['members']
end
def load_chat_history(count: 500)