Class: Twterm::Tab::Statuses::Base

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

Instance Attribute Summary

Attributes inherited from Base

#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 Base

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

Constructor Details

#initialize(app, client) ⇒ Base

Returns a new instance of Base.



73
74
75
76
77
78
79
80
# File 'lib/twterm/tab/statuses/base.rb', line 73

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

  @status_ids = Concurrent::Array.new

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

Instance Method Details

#append(status) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/twterm/tab/statuses/base.rb', line 22

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



33
34
35
36
37
# File 'lib/twterm/tab/statuses/base.rb', line 33

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

#destroy_statusObject



39
40
41
42
43
# File 'lib/twterm/tab/statuses/base.rb', line 39

def destroy_status
  status = highlighted_original_status

  client.destroy_status(status)
end

#drawable_item_countObject



45
46
47
48
49
50
51
52
# File 'lib/twterm/tab/statuses/base.rb', line 45

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



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

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



69
70
71
# File 'lib/twterm/tab/statuses/base.rb', line 69

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

#itemsObject



82
83
84
# File 'lib/twterm/tab/statuses/base.rb', line 82

def items
  statuses
end

#matches?(status, query) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
89
90
91
92
93
94
# File 'lib/twterm/tab/statuses/base.rb', line 86

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


96
97
98
99
100
101
102
103
104
105
106
# File 'lib/twterm/tab/statuses/base.rb', line 96

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



108
109
110
111
112
113
114
115
116
117
# File 'lib/twterm/tab/statuses/base.rb', line 108

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



119
120
121
122
123
# File 'lib/twterm/tab/statuses/base.rb', line 119

def quote
  return if highlighted_status.nil?

  app.tweetbox.quote(highlighted_original_status)
end

#replyObject



125
126
127
128
129
# File 'lib/twterm/tab/statuses/base.rb', line 125

def reply
  return if highlighted_status.nil?

  app.tweetbox.reply(highlighted_original_status)
end

#respond_to_key(key) ⇒ Object



131
132
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
# File 'lib/twterm/tab/statuses/base.rb', line 131

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

  k = KeyMapper.instance

  case key
  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



161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/twterm/tab/statuses/base.rb', line 161

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



176
177
178
179
180
181
182
183
# File 'lib/twterm/tab/statuses/base.rb', line 176

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



185
186
187
188
189
190
191
192
193
# File 'lib/twterm/tab/statuses/base.rb', line 185

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



195
196
197
# File 'lib/twterm/tab/statuses/base.rb', line 195

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

#total_item_countObject



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

def total_item_count
  statuses.count
end