aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorToshiaki Asai <toshi.alternative@gmail.com>2016-12-20 09:46:44 +0900
committerToshiaki Asai <toshi.alternative@gmail.com>2016-12-20 10:58:50 +0900
commit4435cfe373f9c5f939b3d47800000bd5ab89791e (patch)
tree817c5f6804da379951eb50ad49365b2cda175319
parentdaa2ee08da08e8fa24015ab1a4050eff63bdbf5b (diff)
downloadmikutter-4435cfe373f9c5f939b3d47800000bd5ab89791e.tar.gz
ツイートのEntityが欠落している場合にそれっぽいEntityを足す refs #886
-rw-r--r--core/directmessage.rb2
-rw-r--r--core/lib/retriever.rb2
-rw-r--r--core/lib/retriever/entity/basic_twitter_entity.rb (renamed from core/lib/retriever/entity/twitter_entity.rb)9
-rw-r--r--core/lib/retriever/entity/extended_twitter_entity.rb27
-rw-r--r--core/message.rb13
-rw-r--r--core/mui/cairo_markup_generator.rb6
-rw-r--r--test/core/lib/retriever/entity/test_twitter_entity.rb2
7 files changed, 43 insertions, 18 deletions
diff --git a/core/directmessage.rb b/core/directmessage.rb
index eb6bb5df..70b9d390 100644
--- a/core/directmessage.rb
+++ b/core/directmessage.rb
@@ -18,7 +18,7 @@ module Mikutter::Twitter
field.bool :exact # true if complete data
field.time :created # posted time
- entity_class Retriever::Entity::TwitterEntity
+ entity_class Retriever::Entity::ExtendedTwitterEntity
def self.memory
@memory ||= DirectMessageMemory.new end
diff --git a/core/lib/retriever.rb b/core/lib/retriever.rb
index 9fa479e8..f769ce8d 100644
--- a/core/lib/retriever.rb
+++ b/core/lib/retriever.rb
@@ -34,5 +34,5 @@ require_relative 'retriever/model/identity'
require_relative 'retriever/model/memory'
require_relative 'retriever/entity/blank_entity'
require_relative 'retriever/entity/regexp_entity'
-require_relative 'retriever/entity/twitter_entity'
+require_relative 'retriever/entity/extended_twitter_entity'
require_relative 'retriever/entity/url_entity'
diff --git a/core/lib/retriever/entity/twitter_entity.rb b/core/lib/retriever/entity/basic_twitter_entity.rb
index 5105d742..98e558bf 100644
--- a/core/lib/retriever/entity/twitter_entity.rb
+++ b/core/lib/retriever/entity/basic_twitter_entity.rb
@@ -1,13 +1,18 @@
# -*- coding: utf-8 -*-
-require_relative 'blank_entity'
+require_relative 'regexp_entity'
module Retriever::Entity
- class TwitterEntity < BlankEntity
+ class BasicTwitterEntity < RegexpEntity
ESCAPE_RULE = {'&' => '&amp;'.freeze ,'>' => '&gt;'.freeze, '<' => '&lt;'.freeze}.freeze
UNESCAPE_RULE = ESCAPE_RULE.invert.freeze
# REGEXP_EACH_CHARACTER = //u.freeze
REGEXP_ENTITY_ENCODE_TARGET = Regexp.union(*ESCAPE_RULE.keys.map(&Regexp.method(:escape))).freeze
REGEXP_ENTITY_DECODE_TARGET = Regexp.union(*ESCAPE_RULE.values.map(&Regexp.method(:escape))).freeze
+ # screen nameにマッチする正規表現
+ MentionMatcher = /(?:@|@|〄|☯|⑨|♨)([a-zA-Z0-9_]+)/.freeze
+
+ # screen nameのみから構成される文字列から、@などを切り取るための正規表現
+ MentionExactMatcher = /\A(?:@|@|〄|☯|⑨|♨)?([a-zA-Z0-9_]+)\Z/.freeze
def initialize(*_rest)
super
diff --git a/core/lib/retriever/entity/extended_twitter_entity.rb b/core/lib/retriever/entity/extended_twitter_entity.rb
new file mode 100644
index 00000000..17ae4a75
--- /dev/null
+++ b/core/lib/retriever/entity/extended_twitter_entity.rb
@@ -0,0 +1,27 @@
+# -*- coding: utf-8 -*-
+require_relative 'basic_twitter_entity'
+
+module Retriever::Entity
+ ExtendedTwitterEntity = BasicTwitterEntity.filter(
+ Retriever::Entity::BasicTwitterEntity::MentionMatcher, generator: ->h{
+ sn = Retriever::Entity::BasicTwitterEntity::MentionExactMatcher.match(h[:url])[1]
+ user = Retriever::Model(:twitter_user)
+ if user
+ h[:open] = user.findbyidname(sn, Retriever::DataSource::USE_LOCAL_ONLY) ||
+ Retriever::URI.new("https://twitter.com/#{sn}")
+ else
+ h[:open] = Retriever::URI.new("https://twitter.com/#{sn}")
+ end
+ h
+ }).filter(
+ URI.regexp(%w[http https]), generator: ->h{
+ h.merge(open: h[:url])
+ }).filter(
+ /(?:#|#)[a-zA-Z0-9_]+/, generator: ->h{
+ twitter_search = Retriever::Model(:twitter_search)
+ if twitter_search
+ h[:open] = twitter_search.new(query: "##{h[:url][1..h[:url].size]}") end
+ h
+ })
+
+end
diff --git a/core/message.rb b/core/message.rb
index 6e4fdf44..e6d75581 100644
--- a/core/message.rb
+++ b/core/message.rb
@@ -12,12 +12,6 @@ miquire :lib, 'typed-array', 'timelimitedqueue'
投稿1つを表すクラス。
=end
class Message < Retriever::Model
- # screen nameにマッチする正規表現
- MentionMatcher = /(?:@|@|〄|☯|⑨|♨)([a-zA-Z0-9_]+)/.freeze
-
- # screen nameのみから構成される文字列から、@などを切り取るための正規表現
- MentionExactMatcher = /\A(?:@|@|〄|☯|⑨|♨)?([a-zA-Z0-9_]+)\Z/.freeze
-
PermalinkMatcher = Regexp.union(
%r[\Ahttps?://twitter.com/(?:#!/)?(?<screen_name>[a-zA-Z0-9_]+)/status(?:es)?/(?<id>\d+)(?:\?.*)?\Z], # Twitter
%r[\Ahttp://favstar\.fm/users/(?<screen_name>[a-zA-Z0-9_]+)/status/(?<id>\d+)], # Hey, Favstar. Ban stop me premiamu!
@@ -60,8 +54,7 @@ class Message < Retriever::Model
field.time :created # posted time
field.time :modified # updated time
- entity_class Retriever::Entity::TwitterEntity
-
+ entity_class Retriever::Entity::ExtendedTwitterEntity
handle PermalinkMatcher do |uri|
match = PermalinkMatcher.match(uri.to_s)
notice match.inspect
@@ -205,7 +198,7 @@ class Message < Retriever::Model
self[:receiver] = parallel{
self[:receiver] = User.findbyid(receiver_id) }
else
- match = MentionMatcher.match(self[:message].to_s)
+ match = Retriever::Entity::BasicTwitterEntity::MentionMatcher.match(self[:message].to_s)
if match
result = User.findbyidname(match[1])
self[:receiver] = result if result end end end
@@ -221,7 +214,7 @@ class Message < Retriever::Model
# ==== Return
# 宛てられたユーザの idname(screen_name) の配列
def receive_user_screen_names
- self[:message].to_s.scan(MentionMatcher).map(&:first) end
+ self[:message].to_s.scan(Retriever::Entity::BasicTwitterEntity::MentionMatcher).map(&:first) end
# 自分がこのMessageにリプライを返していればtrue
def mentioned_by_me?
diff --git a/core/mui/cairo_markup_generator.rb b/core/mui/cairo_markup_generator.rb
index e84ff1f8..f2772df3 100644
--- a/core/mui/cairo_markup_generator.rb
+++ b/core/mui/cairo_markup_generator.rb
@@ -8,7 +8,7 @@ module Pango
# テキストをPango.parse_markupで安全にパースできるようにエスケープする。
def escape(text)
- text.gsub(/[<>&]/){|m| Retriever::Entity::TwitterEntity::ESCAPE_RULE[m] } end
+ text.gsub(/[<>&]/){|m| Retriever::Entity::BasicTwitterEntity::ESCAPE_RULE[m] } end
alias old_parse_markup parse_markup
@@ -26,8 +26,8 @@ module Pango
module Gdk::MarkupGenerator
- ESCAPE_KEYS = Regexp::union(*Retriever::Entity::TwitterEntity::ESCAPE_RULE.keys)
- ESCAPE_KV = Retriever::Entity::TwitterEntity::ESCAPE_RULE.method(:[])
+ ESCAPE_KEYS = Regexp::union(*Retriever::Entity::BasicTwitterEntity::ESCAPE_RULE.keys)
+ ESCAPE_KV = Retriever::Entity::BasicTwitterEntity::ESCAPE_RULE.method(:[])
# 本文を返す
def main_text
diff --git a/test/core/lib/retriever/entity/test_twitter_entity.rb b/test/core/lib/retriever/entity/test_twitter_entity.rb
index c1822d6f..72da1446 100644
--- a/test/core/lib/retriever/entity/test_twitter_entity.rb
+++ b/test/core/lib/retriever/entity/test_twitter_entity.rb
@@ -10,7 +10,7 @@ class TC_TwitterEntity < Test::Unit::TestCase
field.time :created
field.time :modified
- entity_class Retriever::Entity::TwitterEntity
+ entity_class Retriever::Entity::ExtendedTwitterEntity
def to_show
self[:message]