Debian Codegraph @

plugin/smartthread/smartthread.rb

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2
3 require 'set'
4
5 Plugin.create :smartthread do
6
7 counter = gen_counter 1
8 @timeline_slugs = Set.new # [Symbol]
9
10 # messagesの中で、タイムライン _slug_ に入れるべきものがあれば入れる
11 # ==== Args
12 # [timeline] タイムライン
13 # [messages] 入れるMessageの配列
14 def scan(i_timeline, messages)
15 SerialThread.new do
16 messages.each{ |message|
17 message.each_ancestor { |cur|
18 if i_timeline.include? cur
19 i_timeline << message
20 end
21 }
22 }
23 end
24 end
25
26 command(:smartthread,
27 name: _('会話スレッドを表示'),
28 icon: Skin[:list],
29 condition: lambda{ |opt| not opt.messages.empty? and opt.messages.all? &:repliable? },
30 visible: true,
31 role: :timeline){ |opt|
32 Plugin.call(:open_smartthread, opt.messages)
33 }
34
35 on_open_smartthread do |messages|
36 serial = counter.call
37 slug = "conversation#{serial}".to_sym
38 tab slug, _("会話%{serial_id}") % {serial_id: serial} do
39 set_deletable true
40 set_icon Skin[:list]
41 temporary_tab
42 timeline slug do
43 order do |message|
44 message[:created].to_i end end end
45 @timeline_slugs << slug
46 i_timeline = timeline(slug)
47 i_timeline << messages
48 Delayer::Deferred.when(*messages.map{|m| around_message(m) }).next{ |around_all|
49 i_timeline << around_all.flatten
50 }
51 timeline(slug).active!
52 end
53
54 onappear do |messages|
55 @timeline_slugs.map{|s| timeline(s) }.each do |i_timeline|
56 scan i_timeline, messages
57 end
58 end
59
60 # 引用ツイートをsmartthreadに含める処理。
61 # 会話に新しいMessageが登録される時点で、そのMessageの引用ツイートを取得して格納していく
62 on_gui_timeline_add_messages do |widget, messages|
63 if @timeline_slugs.include?(widget.slug)
64 messages.deach do |message|
65 message.quoting_messages_d(true).next{|quoting_messages|
66 widget << widget.not_in_message(quoting_messages) unless quoting_messages.empty?
67 }.terminate(_('引用ツイートが取得できませんでした'))
68 widget << widget.not_in_message(message.quoted_by) if message.quoted_by?
69 end
70 end
71 end
72
73 on_gui_timeline_add_messages do |widget, messages|
74 if widget.is_a?(Plugin::GUI::Timeline) && !@timeline_slugs.include?(widget.slug)
75 Delayer::Deferred.when(
76 *messages.select(&:has_receive_message?).map do |m|
77 m.replyto_source_d(true).next do |parent|
78 [parent, m]
79 end.trap do |err|
80 Delayer::Deferred.fail(err) if err
81 end
82 end
83 ).next do |result|
84 graph = result.compact.group_by { |p, _| p }.transform_values! do |v|
85 v.map { |_, c| c }.freeze
86 end
87 if !graph.empty?
88 @timeline_slugs.map { |s| timeline(s) }.each do |tl|
89 tl.in_message(graph.keys).each do |parent|
90 tl << graph[parent]
91 end
92 end
93 end
94 end.terminate
95 end
96 end
97
98 on_gui_destroy do |widget|
99 if widget.is_a? Plugin::GUI::Timeline
100 @timeline_slugs.delete(widget.slug)
101 end
102 end
103
104 end