| 1 |
|
| 2 |
|
| 3 |
require_relative 'model/setting' |
| 4 |
|
| 5 |
module Plugin::Extract |
| 6 |
class ConditionNotFoundError < RuntimeError; end |
| 7 |
|
| 8 |
ExtensibleCondition = Struct.new(:slug, :name, :operator, :args) do |
| 9 |
def initialize(*_args, &block) |
| 10 |
super |
| 11 |
@block = block end |
| 12 |
|
| 13 |
def to_proc |
| 14 |
@block end |
| 15 |
|
| 16 |
def call(*_args, **_named, &block) |
| 17 |
@block.(*_args, **_named, &block) end end |
| 18 |
|
| 19 |
class ExtensibleSexpCondition < ExtensibleCondition |
| 20 |
attr_reader :sexp |
| 21 |
|
| 22 |
def initialize(*args) |
| 23 |
@sexp = args.pop |
| 24 |
super(*args) end |
| 25 |
end |
| 26 |
|
| 27 |
ExtensibleOperator = Struct.new(:slug, :name, :args) do |
| 28 |
def initialize(*_args, &block) |
| 29 |
super |
| 30 |
@block = block end |
| 31 |
|
| 32 |
def to_proc |
| 33 |
@block end |
| 34 |
|
| 35 |
def call(*_args, **_named, &block) |
| 36 |
@block.(*_args, **_named, &block) end end |
| 37 |
|
| 38 |
Order = Struct.new(:slug, :name, :ordering) |
| 39 |
|
| 40 |
class Calc |
| 41 |
def self.inherited(child) |
| 42 |
child.class_eval do |
| 43 |
operators = Plugin.filtering(:extract_operator, Set.new).first |
| 44 |
operators.each { |operator| |
| 45 |
define_method(operator.slug) do |other| |
| 46 |
@condition.(other, message: @message, operator: operator.slug, &operator) |
| 47 |
end |
| 48 |
} |
| 49 |
end |
| 50 |
end |
| 51 |
|
| 52 |
def initialize(message, condition) |
| 53 |
type_strict condition => Plugin::Extract::ExtensibleCondition |
| 54 |
@message, @condition = message, condition |
| 55 |
end |
| 56 |
|
| 57 |
def call(*args) |
| 58 |
@condition.(*args, message: @message) |
| 59 |
end |
| 60 |
end |
| 61 |
end |
| 62 |
|
| 63 |
Plugin.create :extract do |
| 64 |
オブジェクトの集合。 |
| 65 |
、以下のようなフィールドを持っている必要がある。 |
| 66 |
される、抽出タブの名前 |
| 67 |
ソーススラッグ |
| 68 |
defevent :message_stream, prototype: [Pluggaloid::COLLECT] |
| 69 |
|
| 70 |
defevent :extract_receive_message, prototype: [Plugin::Extract::Setting, Pluggaloid::STREAM] |
| 71 |
defevent :extract_order, prototype: [Pluggaloid::COLLECT] |
| 72 |
|
| 73 |
@load_time = Time.new.freeze |
| 74 |
|
| 75 |
ーは抽出タブslugで、値は以下のようなオブジェクト |
| 76 |
ブの名前 |
| 77 |
式(S式) |
| 78 |
見るか(イベント名、配列で複数) |
| 79 |
インとタブのスラッグ |
| 80 |
def extract_tabs |
| 81 |
@extract_tabs ||= {} end |
| 82 |
|
| 83 |
def message_created_after_load_proc |
| 84 |
@message_created_after_load_proc ||= ->(message) { message.created >= @load_time } |
| 85 |
end |
| 86 |
|
| 87 |
command(:extract_edit, |
| 88 |
name: _('抽出条件を編集'), |
| 89 |
condition: lambda{ |opt| |
| 90 |
extract_tabs.values.any? { |es| es.slug == opt.widget.slug } |
| 91 |
}, |
| 92 |
visible: true, |
| 93 |
role: :tab) do |opt| |
| 94 |
extract = extract_tabs.values.find { |es| es.slug == opt.widget.slug } |
| 95 |
Plugin.call(:extract_open_edit_dialog, extract.slug) if extract |
| 96 |
end |
| 97 |
|
| 98 |
defdsl :defextractcondition do |slug, name: raise, operator: true, args: 0, sexp: nil, &block| |
| 99 |
if sexp |
| 100 |
filter_extract_condition do |conditions| |
| 101 |
conditions << Plugin::Extract::ExtensibleSexpCondition.new(slug, name, operator, args, sexp).freeze |
| 102 |
[conditions] end |
| 103 |
else |
| 104 |
filter_extract_condition do |conditions| |
| 105 |
conditions << Plugin::Extract::ExtensibleCondition.new(slug, name, operator, args, &block).freeze |
| 106 |
[conditions] end end end |
| 107 |
|
| 108 |
defdsl :defextractoperator do |slug, name: raise, args: 1, &block| |
| 109 |
filter_extract_operator do |operators| |
| 110 |
operators << Plugin::Extract::ExtensibleOperator.new(slug, name, args, &block).freeze |
| 111 |
[operators] end end |
| 112 |
|
| 113 |
defdsl :defextractorder do |slug, name:, &block| |
| 114 |
slug = slug.to_sym |
| 115 |
name = name.to_s.freeze |
| 116 |
collection(:extract_order) do |mutation| |
| 117 |
mutation.add(Plugin::Extract::Order.new(slug, name, block)) |
| 118 |
end |
| 119 |
end |
| 120 |
|
| 121 |
defextractoperator(:==, name: _('='), args: 1, &:==) |
| 122 |
defextractoperator(:!=, name: _('≠'), args: 1, &:!=) |
| 123 |
defextractoperator(:match_regexp, name: _('正規表現'), args: 1, &:match_regexp) |
| 124 |
defextractoperator(:include?, name: _('含む'), args: 1, &:include?) |
| 125 |
|
| 126 |
defextractcondition(:user, name: _('ユーザ名'), operator: true, args: 1, sexp: MIKU.parse("`(,compare (idname (user message)) ,(car args))")) |
| 127 |
|
| 128 |
defextractcondition(:body, name: _('本文'), operator: true, args: 1, sexp: MIKU.parse("`(,compare (description message) ,(car args))")) |
| 129 |
|
| 130 |
defextractcondition(:source, name: _('投稿したクライアントアプリケーション名'), operator: true, args: 1, sexp: MIKU.parse("`(,compare (to_s (fetch message 'source)) ,(car args))")) |
| 131 |
|
| 132 |
defextractcondition(:receiver_idnames, name: _('宛先ユーザ名のいずれか一つ以上'), operator: true, args: 1) do |arg, message: raise, operator: raise, &compare| |
| 133 |
message.receive_user_idnames.any? do |sn| |
| 134 |
compare.(sn, arg) |
| 135 |
end |
| 136 |
end |
| 137 |
|
| 138 |
defextractorder(:created, name: _('投稿時刻')) do |model| |
| 139 |
model.created.to_i |
| 140 |
end |
| 141 |
|
| 142 |
defextractorder(:modified, name: _('投稿時刻 (ふぁぼやリツイートでageる)')) do |model| |
| 143 |
model.modified.to_i |
| 144 |
end |
| 145 |
|
| 146 |
on_extract_tab_create do |setting| |
| 147 |
if setting.is_a?(Hash) |
| 148 |
setting = Plugin::Extract::Setting.new(setting) |
| 149 |
end |
| 150 |
extract_tabs[setting.slug] = setting |
| 151 |
tab(setting.slug, setting.name) do |
| 152 |
set_icon setting.icon.to_s if setting.icon? |
| 153 |
timeline setting.slug do |
| 154 |
oo = setting.find_ordering_obj |
| 155 |
order(&setting.find_ordering_obj.ordering) if oo |
| 156 |
end |
| 157 |
end |
| 158 |
modify_extract_tabs end |
| 159 |
|
| 160 |
on_extract_tab_update do |setting| |
| 161 |
extract_tabs[setting.slug] = setting |
| 162 |
tab(setting.slug).set_icon setting.icon.to_s if setting.icon? |
| 163 |
oo = setting.find_ordering_obj |
| 164 |
timeline(setting.slug).order(&oo.ordering) if oo |
| 165 |
modify_extract_tabs end |
| 166 |
|
| 167 |
on_extract_tab_delete do |slug| |
| 168 |
if extract_tabs.has_key? slug |
| 169 |
deleted_tab = extract_tabs[slug] |
| 170 |
tab(deleted_tab.slug).destroy |
| 171 |
extract_tabs.delete(slug) |
| 172 |
modify_extract_tabs end end |
| 173 |
|
| 174 |
ge_streamを使う |
| 175 |
filter_extract_datasources do |ds_dict| |
| 176 |
[{ **ds_dict, |
| 177 |
**collect(:message_stream).map { |ms| [ms.datasource_slug.to_sym, ms.title] }.to_h |
| 178 |
}] |
| 179 |
end |
| 180 |
|
| 181 |
filter_extract_tabs_get do |tabs| |
| 182 |
[tabs + extract_tabs.values] |
| 183 |
end |
| 184 |
|
| 185 |
filter_active_datasources do |ds| |
| 186 |
[ds + active_datasources] |
| 187 |
end |
| 188 |
|
| 189 |
現在の内容を保存する |
| 190 |
def modify_extract_tabs |
| 191 |
UserConfig[:extract_tabs] = extract_tabs.values.map(&:export_to_userconfig) |
| 192 |
old_subscribers = @datasource_subscribers |
| 193 |
@datasource_subscribers = handler_tag do |
| 194 |
extract_tabs.values.each do |tab| |
| 195 |
tl = timeline(tab.slug) |
| 196 |
tab.sources.map { |source| |
| 197 |
subscribe(:extract_receive_message, source) |
| 198 |
}&.yield_self { |a, *rest| |
| 199 |
rest.empty? ? a : a.merge(*rest) |
| 200 |
}&.map { |message| |
| 201 |
message.retweet_source || message |
| 202 |
}&.select(&compile(tab.slug, tab.sexp)) |
| 203 |
&.reject(&tl.method(:include?)) |
| 204 |
&.each(&tl.method(:<<)) |
| 205 |
if tab.popup? |
| 206 |
subscribe(:gui_timeline_add_messages, tl).select(&message_created_after_load_proc).each do |message| |
| 207 |
Plugin.call(:popup_notify, message.user, message.description) |
| 208 |
end |
| 209 |
end |
| 210 |
if tab.sound&.yield_self { |sound_uri| FileTest.exist?(sound_uri.to_s) } |
| 211 |
subscribe(:gui_timeline_add_messages, tl).select(&message_created_after_load_proc).each do |
| 212 |
Plugin.call(:play_sound, tab.sound.to_s) |
| 213 |
end |
| 214 |
end |
| 215 |
end |
| 216 |
end |
| 217 |
old_subscribers&.yield_self(&method(:detach)) |
| 218 |
self |
| 219 |
end |
| 220 |
|
| 221 |
るデータソースのSetを返す |
| 222 |
def active_datasources |
| 223 |
@active_datasources ||= |
| 224 |
extract_tabs.values.map{|tab| |
| 225 |
tab.sources |
| 226 |
}.inject(Set.new, &:merge).freeze |
| 227 |
end |
| 228 |
|
| 229 |
def compile(tab_slug, code) |
| 230 |
if code.empty? |
| 231 |
:itself.to_proc |
| 232 |
else |
| 233 |
begin |
| 234 |
before = Set.new |
| 235 |
extract_condition ||= Plugin.filtering(:extract_condition, []).first.to_h do |condition| |
| 236 |
[condition.slug, condition] |
| 237 |
end |
| 238 |
evaluated = |
| 239 |
MIKU::Primitive.new(:to_ruby_ne).call( |
| 240 |
MIKU::SymbolTable.new, |
| 241 |
metamorphose( |
| 242 |
code: code, |
| 243 |
assign: before, |
| 244 |
extract_condition: extract_condition |
| 245 |
) |
| 246 |
) |
| 247 |
source = ['->(message) do', *before, evaluated, 'end'].join("\n") |
| 248 |
instance_eval(source) |
| 249 |
rescue SyntaxError |
| 250 |
error "compiled code: #{source}" |
| 251 |
raise |
| 252 |
rescue Plugin::Extract::ConditionNotFoundError => exception |
| 253 |
Plugin.call(:modify_activity, |
| 254 |
plugin: self, |
| 255 |
kind: 'error', |
| 256 |
title: _('抽出タブ条件エラー'), |
| 257 |
date: Time.new, |
| 258 |
description: _("抽出タブ「%{tab_name}」で使われている条件が見つかりませんでした:\n%{error_string}") % {tab_name: extract_tabs[tab_slug].name, error_string: exception.to_s}) |
| 259 |
warn exception |
| 260 |
:itself.to_proc |
| 261 |
end |
| 262 |
end |
| 263 |
end |
| 264 |
|
| 265 |
、くいっと変形させてな |
| 266 |
def metamorphose(code: raise, assign: Set.new, extract_condition: nil) |
| 267 |
extract_condition ||= Plugin.filtering(:extract_condition, []).first.to_h do |condition| |
| 268 |
[condition.slug, condition] |
| 269 |
end |
| 270 |
case code |
| 271 |
when MIKU::Atom |
| 272 |
return code |
| 273 |
when MIKU::List |
| 274 |
return true if code.empty? |
| 275 |
condition = if code.size <= 2 |
| 276 |
extract_condition[code.car] |
| 277 |
else |
| 278 |
extract_condition[code.cdr.car] end |
| 279 |
case condition |
| 280 |
when Plugin::Extract::ExtensibleSexpCondition |
| 281 |
metamorphose_sexp(code: code, condition: condition) |
| 282 |
when Plugin::Extract::ExtensibleCondition |
| 283 |
assign << "#{condition.slug} = Class.new(Plugin::Extract::Calc).new(message, extract_condition[:#{condition.slug}])" |
| 284 |
if condition.operator |
| 285 |
code |
| 286 |
else |
| 287 |
|
| 288 |
[:call, condition.slug] |
| 289 |
end |
| 290 |
else |
| 291 |
if code.cdr.car.is_a? Symbol and not %i[and or not].include?(code.car) |
| 292 |
raise Plugin::Extract::ConditionNotFoundError, _('抽出条件 `%{condition}\' が見つかりませんでした') % {condition: code.cdr.car} end |
| 293 |
code.map{|node| metamorphose(code: node, |
| 294 |
assign: assign, |
| 295 |
extract_condition: extract_condition) } end end end |
| 296 |
|
| 297 |
def metamorphose_sexp(code: raise, condition: raise) |
| 298 |
miku_context = MIKU::SymbolTable.new |
| 299 |
miku_context[:compare] = MIKU::Cons.new(code.car, nil) |
| 300 |
miku_context[:args] = MIKU::Cons.new(code.cdr.cdr, nil) |
| 301 |
begin |
| 302 |
miku(condition.sexp, miku_context) |
| 303 |
rescue => exception |
| 304 |
error "error occurred in code #{MIKU.unparse(condition.sexp)}" |
| 305 |
error miku_context |
| 306 |
raise exception end end |
| 307 |
|
| 308 |
(UserConfig[:extract_tabs] or []).each do |record| |
| 309 |
Plugin.call(:extract_tab_create, Plugin::Extract::Setting.new(record)) |
| 310 |
end |
| 311 |
|
| 312 |
on_userconfig_modify do |key, val| |
| 313 |
next if key != :extract_tabs |
| 314 |
@active_datasources = nil |
| 315 |
end |
| 316 |
end |