Class: Twterm::Tab::Statuses::AbstractStatusesTab

Inherits:
AbstractTab
  • Object
show all
Includes:
Publisher, Subscriber, Loadable, Twterm::Tab::Searchable, Utils
Defined in:
lib/twterm/tab/statuses/abstract_statuses_tab.rb

Instance Attribute Summary

Attributes inherited from AbstractTab

#title, #window

Instance Method Summary collapse

Methods included from Utils

check_type

Methods included from Loadable

#initially_loaded!, #initially_loaded?

Methods included from Subscriber

included, #subscribe, #unsubscribe

Methods included from Twterm::Tab::Scrollable

#scroller

Methods included from Publisher

#publish

Methods inherited from AbstractTab

#==, #close, #find_or_fetch_list, #find_or_fetch_status, #find_or_fetch_user, #render

Constructor Details

#initialize(app, client) ⇒ AbstractStatusesTab

Returns a new instance of AbstractStatusesTab.



75
76
77
78
79
80
81
82
# File 'lib/twterm/tab/statuses/abstract_statuses_tab.rb', line 75

def initialize(app, client)
  super(app, client)

  @status_ids = Concurrent::Array.new

  subscribe(Event::StatusDeleted) { |e| delete(e.status_id) }
  subscribe(Event::StatusGarbageCollected) { |e| @status_ids.delete(e.id) }
end

Instance Method Details

#append(status) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/twterm/tab/statuses/abstract_statuses_tab.rb', line 24

def append(status)
  check_type Status, status

  return if @status_ids.include?(status.id)

  @status_ids.push(status.id)
  status.split(window.maxx - 4)
  scroller.item_appended!
  render
end

#delete(status_id) ⇒ Object



35
36
37
38
39
# File 'lib/twterm/tab/statuses/abstract_statuses_tab.rb', line 35

def delete(status_id)
  app.status_repository.delete(status_id)
  @status_ids.delete(status_id)
  render
end

#destroy_statusObject



41
42
43
44
45
# File 'lib/twterm/tab/statuses/abstract_statuses_tab.rb', line 41

def destroy_status
  status = highlighted_original_status

  client.destroy_status(status)
end

#drawable_item_countObject



47
48
49
50
51
52
53
54
# File 'lib/twterm/tab/statuses/abstract_statuses_tab.rb', line 47

def drawable_item_count
  statuses.drop(scroller.offset).lazy
  .map { |s| s.split(window.maxx - 4).count + 2 }
  .scan(0, :+)
  .each_cons(2)
  .select { |_, l| l < window.maxy }
  .count
end

#favoriteObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/twterm/tab/statuses/abstract_statuses_tab.rb', line 56

def favorite
  status = highlighted_original_status

  return if status.nil?

  if status.favorited?
    client.unfavorite(status)
      .then { status.unfavorite! }
  else
    client.favorite(status)
      .then { status.favorite! }
  end
    .then { render }
end

#fetchObject



71
72
73
# File 'lib/twterm/tab/statuses/abstract_statuses_tab.rb', line 71

def fetch
  fail NotImplementedError, 'fetch method must be implemented'
end

#itemsObject



84
85
86
# File 'lib/twterm/tab/statuses/abstract_statuses_tab.rb', line 84

def items
  statuses
end

#matches?(status, query) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
91
92
93
94
95
96
# File 'lib/twterm/tab/statuses/abstract_statuses_tab.rb', line 88

def matches?(status, query)
  user = app.user_repository.find(status.user_id)

  [
    status.text,
    user.screen_name,
    user.name
  ].any? { |x| x.downcase.include?(query.downcase) }
end


98
99
100
101
102
103
104
105
106
107
108
# File 'lib/twterm/tab/statuses/abstract_statuses_tab.rb', line 98

def open_link
  status = highlighted_original_status

  return if status.nil?

  urls = status.urls.map(&:expanded_url) + status.media.map(&:expanded_url)
  urls
    .uniq
    .map { |url| Event::OpenURI.new(url) }
    .each { |e| publish(e) }
end

#prepend(status) ⇒ Object



110
111
112
113
114
115
116
117
118
119
# File 'lib/twterm/tab/statuses/abstract_statuses_tab.rb', line 110

def prepend(status)
  fail unless status.is_a? Status

  return if @status_ids.include?(status.id)

  @status_ids.unshift(status.id)
  status.split(window.maxx - 4)
  scroller.item_prepended!
  render
end

#quoteObject



121
122
123
124
125
# File 'lib/twterm/tab/statuses/abstract_statuses_tab.rb', line 121

def quote
  return if highlighted_status.nil?

  app.tweetbox.quote(highlighted_original_status)
end

#replyObject



127
128
129
130
131
# File 'lib/twterm/tab/statuses/abstract_statuses_tab.rb', line 127

def reply
  return if highlighted_status.nil?

  app.tweetbox.reply(highlighted_original_status)
end

#respond_to_key(key) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/twterm/tab/statuses/abstract_statuses_tab.rb', line 133

def respond_to_key(key)
  return true if scroller.respond_to_key(key)

  k = KeyMapper.instance

  case key
  when 10
    open_status_tab
  when k[:status, :conversation]
    show_conversation
  when k[:status, :destroy]
    destroy_status
  when k[:status, :like]
    favorite
  when k[:status, :open_link]
    open_link
  when k[:status, :reply]
    reply
  when k[:status, :retweet]
    retweet
  when k[:tab, :reload]
    reload
  when k[:status, :quote]
    quote
  when k[:status, :user]
    show_user
  else
    return false
  end
  true
end

#retweetObject



165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/twterm/tab/statuses/abstract_statuses_tab.rb', line 165

def retweet
  status = highlighted_original_status

  return if status.nil?

  if status.retweeted?
    client.unretweet(status)
      .then { status.unretweet! }
  else
    client.retweet(status)
      .then { status.retweet! }
  end
    .then { render }
end

#show_conversationObject



180
181
182
183
184
185
186
187
# File 'lib/twterm/tab/statuses/abstract_statuses_tab.rb', line 180

def show_conversation
  status = highlighted_original_status

  return if status.nil?

  tab = Tab::Statuses::Conversation.new(app, client, highlighted_original_status.id)
  app.tab_manager.add_and_show(tab)
end

#show_userObject



189
190
191
192
193
194
195
196
197
# File 'lib/twterm/tab/statuses/abstract_statuses_tab.rb', line 189

def show_user
  status = highlighted_original_status

  return if status.nil?

  user_id = status.user_id
  user_tab = Tab::UserTab.new(app, client, user_id)
  app.tab_manager.add_and_show(user_tab)
end

#statusesObject



199
200
201
# File 'lib/twterm/tab/statuses/abstract_statuses_tab.rb', line 199

def statuses
  @status_ids.map { |id| app.status_repository.find(id) }.compact
end

#total_item_countObject



203
204
205
# File 'lib/twterm/tab/statuses/abstract_statuses_tab.rb', line 203

def total_item_count
  statuses.count
end