-
Notifications
You must be signed in to change notification settings - Fork 3
/
entry_helper.rb
1288 lines (1173 loc) · 34.3 KB
/
entry_helper.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
module EntryHelper
BRIGHTKITE_MAP_ZOOM = 12
BRIGHTKITE_MAP_WIDTH = 120
BRIGHTKITE_MAP_HEIGHT = 80
def top_menu
if ctx.list?
super
else
[
write_new_link,
search_link
].join
end
end
def viewname
if ctx.service
viewname_base + "(#{ctx.service})"
else
viewname_base
end
end
def viewname_base
return ctx.viewname if ctx.viewname
if ctx.eid
'entry'
elsif ctx.query
'search results'
elsif ctx.like == 'likes'
"entries #{user_name(ctx.user || auth.name)} likes"
elsif ctx.like == 'liked'
"#{user_name(ctx.user || auth.name)}'s liked entries"
elsif ctx.comment == 'discussion'
"#{user_name(ctx.user || auth.name)}'s discussion"
elsif ctx.user
"#{user_name(ctx.user || auth.name)}"
elsif ctx.friends
"#{user_name(ctx.user || auth.name)} with friends"
elsif ctx.list
"'#{list_name(ctx.list)}' entries"
elsif ctx.room
if ctx.room == '*'
'groups'
else
"#{room_name(ctx.room_for)}"
end
elsif ctx.link
'related entries'
elsif ctx.label == 'pin'
'pinned'
elsif ctx.inbox
'inbox'
else
'archived'
end
end
def ctx
@ctx
end
def cache_profile(entries)
users = [auth.name]
rooms = []
# TODO: do not show status icon for query result page now for performance
# reason.
if ctx.query.nil? and ctx.link.nil? and !F2P::Config.disable_status_icon
entries.each do |t|
t.entries.each do |e|
if e.room
rooms << e.room.nickname
else
users << e.nickname if e.nickname
end
end
end
end
@user_status = User.ff_status_map(auth, users.uniq)
@room_status = Room.ff_status_map(auth, rooms.uniq)
end
def link_action(action, opt = {})
{ :controller => 'entry', :action => action }.merge(opt)
end
def link_list(opt = {})
link_action('list', opt)
end
def link_show(id, opt = {})
link_action('show', opt.merge(:id => u(id)))
end
def link_user(user, opt = {})
link_list(opt.merge(:user => user))
end
def author_picture(entry)
return if !setting.list_view_profile_picture
return if ctx.user_for or ctx.room_for
if nickname = entry.origin_nickname
unless imaginary?(nickname)
if nickname == entry.nickname
user_picture(entry.nickname)
else
room_picture(entry.room.nickname)
end
end
end
end
def pin_link(entry)
if entry.view_pinned
link_to(icon_tag(:pinned, 'unpin'), link_action('unpin', :id => entry.id))
else
link_to(icon_tag(:pin), link_action('pin', :id => entry.id))
end
end
def icon(entry)
service = entry.service
return unless service
if ctx.user
user = entry.nickname || entry.user_id
end
room_entry = (entry.room and entry.room.nickname != ctx.room_for)
if room_entry
opt = { :room => u(entry.room.nickname) }
user = nil
elsif entry.room
opt = { :room => u(entry.room.nickname), :service => u(service.id) }
else
opt = { :service => u(service.id) }
end
opt[:label] = ctx.label
link = link_user(user, opt)
if room_entry
str = room_icon(service, entry.room.nickname, link)
else
str = service_icon(service, link)
end
# TODO: do not show status icon for query result page now for performance
# reason.
if ctx.query.nil? and ctx.link.nil? and !ctx.user_for and !F2P::Config.disable_status_icon
if entry_status(entry) != 'public'
str = icon_tag(:private) + str
end
end
str
end
def icon_extra(entry)
room_entry = (entry.room and entry.room.nickname != ctx.room_for)
str = nil
if room_entry
str = entry.room.name
end
if entry.service.service_group?
str = entry.service.name
end
if str
h("(#{str})")
end
end
def media_disabled?
ctx.list? and !setting.list_view_media_rendering
end
def media_tag(entry, url, opt = {})
if entry and media_disabled?
link_to(icon_tag(:media_disabled), link_show(entry.id))
else
image_tag(url, opt.merge(:class => h('media')))
end
end
def content(entry)
common = common_content(entry)
if entry.service.nil?
content = common
elsif entry.service.twitter?
content = twitter_content(common, entry)
elsif entry.service.tumblr?
content = tumblr_content(common, entry)
elsif entry.service.stumbleupon?
content = stumbleupon_content(common, entry)
else
content = common
end
scan_media_from_link(entry)
unless entry.view_medias.empty?
content += media_indent
entry.view_medias.each do |uri|
content += media_tag(entry, uri)
end
end
content
end
def common_content(entry)
title = entry.title
return unless title
if entry.link and with_link?(entry.service)
content = link_content(title, entry)
else
fold, str, links = escape_text(title, ctx.fold ? setting.text_folding_size : nil)
entry.view_links = links
if fold
str += link_to(inline_icon_tag(:more), link_show(entry.id))
end
content = q(str)
end
if !entry.medias.empty?
# entries from Hatena contains 'enclosure' but no title and link for now.
with_media = content_with_media(entry)
content += media_indent + with_media unless with_media.empty?
end
if entry.geo and !entry.view_map
point = GoogleMaps::Point.new(title, entry.geo.lat, entry.geo.long)
zoom = F2P::Config.google_maps_zoom
width = F2P::Config.google_maps_width
height = F2P::Config.google_maps_height
if entry.service.brightkite?
if !entry.medias.empty?
zoom = BRIGHTKITE_MAP_ZOOM
width = BRIGHTKITE_MAP_WIDTH
height = BRIGHTKITE_MAP_HEIGHT
end
end
content += ' ' + google_maps_link(point, entry, zoom, width, height)
end
content
end
def scan_media_from_link(entry)
if entry.view_links and entry.view_medias.empty?
entry.view_links.each do |link|
case link
# http://twitpic.com/api.do#thumbnails
when /\btwitpic.com\b/
if uri = uri(link)
uri.path = "/show/mini#{uri.path}"
entry.view_medias << uri.to_s
end
# via gotoken
when /\bmovapic.com\/pic\/([a-z0-9]+)/
uri = "http://image.movapic.com/pic/t_#{$1}.jpeg"
entry.view_medias << uri
# http://code.google.com/p/imageshackapi/wiki/YFROGthumbnails
when /\byfrog.com\b/
uri = link + '.th.jpg'
entry.view_medias << uri
# http://pic.im/website/api
when /\bpic.im\b/
if uri = uri(link)
uri.path = "/website/thumbnail#{uri.path}"
entry.view_medias << uri.to_s
end
# http://pix.im/api#thumbnails
when /\bpix.im\b/
uri = link + '/thumbnail'
entry.view_medias << uri
end
end
end
end
def media_indent
"<br />\n "
end
def friend_of(entry)
if friend_of = entry.friend_of
h(" (through #{friend_of.name})")
end
end
def author_link(entry)
if !entry.anonymous and !ctx.user_only?
(user(entry) || '') + (friend_of(entry) || '')
end
end
def comment_author_link(comment)
unless comment.posted_with_entry?
h('by ') + user(comment)
end
end
def emphasize_as_unread?(entry_or_comment)
(ctx.home or ctx.inbox or ctx.label) and entry_or_comment.view_unread
end
def original_link(entry)
if entry.link
link_content = 'See original'
if unknown_where_to_go?(entry)
link_content += "(#{uri_domain(entry.link)})"
end
entry_link_to(h(link_content), entry.link)
end
end
def link_content(title, entry)
if unknown_where_to_go?(entry)
entry_link_to(h(title), entry.link) + h(" (#{uri_domain(entry.link)})")
else
entry_link_to(h(title), entry.link)
end
end
def link_content_without_link(title, entry)
q(h(title))
end
def entry_link_to(name, options = {}, html_options = {})
@already_linked ||= false
unless @already_linked
@already_linked = true
html_options = html_options.merge(:id => 'first_link')
end
link_to(name, options, html_options)
end
def uri(str)
begin
uri = URI.parse(str)
uri = nil if uri.host.nil?
uri
rescue
nil
end
end
def uri_domain(str)
uri = uri(str)
if uri
uri.host
else
str
end
end
def unknown_where_to_go?(entry)
link_url = uri(entry.link)
profile_url = uri(entry.service.profile_url)
if profile_url and link_url
(profile_url.host.downcase != link_url.host.downcase)
else
entry.service.service_group? or entry.service.tumblr?
end
end
def with_link?(service)
service and service.entry_type != 'message' and !service.twitter?
end
def content_with_media(entry)
medias = entry.medias
if ctx.single?
display = medias
else
display = medias[0, F2P::Config.medias_in_thread]
end
str = display.collect { |media|
title = media.title
if direct_image_link?(entry, media)
link = extract_first_media_link(media)
else
link = media.link
end
safe_content = nil
tbs = media.thumbnails
encs = media.enclosures
if tbs and !tbs.empty?
if first = select_thumbnail(tbs)
tb_url = first.url
tb_width = first.width
tb_height = first.height
end
end
if !tb_url and display.size == 1 and encs and encs.first
# Google Reader has no thumbnails but enclosures...
if ctx.single? or !cell_phone?
tb_url = encs.first['url']
end
end
if tb_url
label = title || entry.title
size = image_size(tb_width, tb_height) if tb_width and tb_height
safe_content = media_tag(entry, tb_url, :alt => h(label), :title => h(label), :size => size)
elsif title
safe_content = h(title)
else
safe_content = h('[media]')
end
if !media_disabled? and link
link_to(safe_content, link)
else
safe_content
end
}.join(' ')
if medias.size != display.size
msg = " (#{medias.size - display.size} more images)"
str += link_to(icon_tag(:more), link_show(entry.id)) + h(msg)
end
str
end
def select_thumbnail(tbs)
if /movapic/ =~ tbs.first.url
if tb = tbs.find { |e| /t_/ =~ e.url }
return tb
end
end
if tb = tbs.find_all { |e| e.height }.min_by { |e| e.height }
return tb
end
if ctx.single? or !cell_phone?
tbs.first
end
end
def direct_image_link?(entry, media)
if entry.service.internal?
if c = media.contents.first
if tb = media.thumbnails.first
if tb.width == c.width and tb.height == c.height
return false
end
end
end
true
else
false
end
end
def extract_first_media_link(media)
if c = media.contents.first
link = c.url
end
if media.enclosures.first
link ||= media.enclosures.first['url']
end
link ||= media.link
link
end
def google_maps_link(point, entry, zoom = F2P::Config.google_maps_zoom, width = F2P::Config.google_maps_width, height = F2P::Config.google_maps_height)
generator = GoogleMaps::URLGenerator.new(F2P::Config.google_maps_api_key)
lat = point.lat
long = point.long
address = point.address
tb = generator.staticmap_url(F2P::Config.google_maps_maptype, lat, long, :zoom => zoom, :width => width, :height => height)
link = generator.link_url(lat, long, address)
content = media_tag(entry, tb, :alt => h(address), :title => h(address), :size => image_size(width, height))
if media_disabled?
content
else
link_to(content, link)
end
end
def google_maps_markers_link(entries)
generator = GoogleMaps::URLGenerator.new(F2P::Config.google_maps_api_key)
markers = entries.map { |e|
[e.geo.lat, e.geo.long]
}
tb = generator.staticmap_markers_url(F2P::Config.google_maps_maptype, markers, :width => F2P::Config.google_maps_width, :height => F2P::Config.google_maps_height)
ids = entries.sort { |a, b|
a.published_at <=> b.published_at
}.map { |e| e.id }.join(',')
if media_disabled?
content = h('[filter geo entries]')
else
content = media_tag(nil, tb, :size => image_size(F2P::Config.google_maps_width, F2P::Config.google_maps_height))
end
link_to(content, link_list(:ids => ids))
end
def twitter_content(common, entry)
common.gsub(/@([a-zA-Z0-9_]+)/) {
'@' + link_to($1, "http://twitter.com/#{$1}")
}
end
def tumblr_content(common, entry)
title = entry.title
fold = fold_length(title, setting.text_folding_size - 3)
if ctx.fold and entry.medias.empty? and fold != title
link_content(fold + '...', entry) + link_to(inline_icon_tag(:more), link_show(entry.id))
else
common
end
end
def stumbleupon_content(common, entry)
if entry.medias.empty?
common
else
str = entry.medias.collect { |media|
link = media.link
if media.enclosures.first
url = media.enclosures.first['url']
label = entry.title
safe_content = media_tag(entry, url, :alt => h(label), :title => h(label))
end
if safe_content
if !media_disabled? and link
link_to(safe_content, link)
else
safe_content
end
end
}.join(' ')
common + media_indent + str
end
end
def escape_text(content, fold_size = nil)
str = ''
fold_size ||= content.length
org_size = 0
m = nil
links = []
while content.match(URI.regexp(['http', 'https']))
m = $~
added, part = fold_concat(m.pre_match, fold_size - org_size)
str += h(part)
if added
org_size += added
else
return true, str, links
end
target = m[0]
content = m.post_match
if target[-1] == ?)
target[-1, 1] = ''
content = ')' + content
end
uri = uri(target)
added, part = fold_concat(target, fold_size - org_size)
if uri.nil? or !uri.is_a?(URI::HTTP)
str += h(part)
if added
org_size += added
else
return true, str, links
end
else
links << target
if added
str += link_to(h(target), target)
org_size += added
else
str += link_to(h(part), target)
return true, str, links
end
end
end
added, part = fold_concat(content, fold_size - org_size)
str += h(part)
unless added
return true, str, links
end
return false, str, links
end
def fold_concat(str, fold_size)
return 0, str if str.empty?
size = str.scan(/./u).size
if size > fold_size
return nil, fold_length(str, fold_size - 3) + '...'
else
return size, str
end
end
def via(entry_or_comment)
label = entry_or_comment.respond_to?(:service) ? 'from' : 'via'
super(entry_or_comment.via, label)
end
def likes(entry)
me = []
friends = []
rest = []
subscriptions = user_subscriptions(auth.name).inject({}) { |r, e| r[e.nickname] = true; r }
entry.likes.each do |e|
if e.nickname == auth.name
me << e
elsif subscriptions.key?(e.nickname)
friends << e
else
rest << e
end
end
likes = me + friends + rest
if !likes.empty?
if liked?(entry)
icon = link_to(icon_tag(:star, 'unlike'), link_action('unlike', :id => u(entry.id)))
else
icon = icon_tag(:star)
end
icon += h(likes.size.to_s)
max = F2P::Config.max_friend_list_num
if likes.size > max + 1
msg = "... #{likes.size - max} more likes"
members = likes[0, max].collect { |like| user(like) }.join(' ') + ' ' + h(msg)
else
members = likes.collect { |like| user(like) }.join(' ')
end
icon + '(' + members + ')'
end
end
def friends_likes(entry)
subscriptions = user_subscriptions(auth.name).inject({}) { |r, e| r[e.nickname] = true; r }
likes = entry.likes.find_all { |e| subscriptions.key?(e.nickname) }
if !entry.likes.empty?
if liked?(entry)
icon = link_to(icon_tag(:star, 'unlike'), link_action('unlike', :id => u(entry.id)))
else
icon = icon_tag(:star)
end
if entry.likes.size != likes.size
icon += link_to(h(entry.likes.size.to_s), link_show(entry.id))
else
icon += h(entry.likes.size.to_s)
end
if !likes.empty?
members = likes.collect { |like| user(like) }.join(' ')
icon += '(' + members + ')'
end
icon
end
end
def comments(entry, compact)
unless entry.comments.empty?
link_to(icon_tag(:comment) + h(entry.comments.size.to_s), link_show(entry.id))
end
end
def updated(entry, compact)
date(entry.modified, compact)
end
def emphasize_as_unread(str)
content_tag('span', str, :class => 'inbox')
end
def published(entry, compact = false)
str = date(entry.published_at, compact)
if emphasize_as_unread?(entry)
str = emphasize_as_unread(str)
end
str
#if compact
# link_to(str, link_show(entry.id))
#else
# str
#end
end
def modified_if(entry, compact)
if compact
pub = published(entry, compact)
mod = date(entry.modified, compact)
if pub != mod
"(#{mod} up)"
end
end
end
def user(entry_or_comment)
unless entry_or_comment.respond_to?(:service)
return super(entry_or_comment.user)
end
entry = entry_or_comment
if setting.twitter_comment_hack and entry.service.twitter?
if nickname = entry.nickname
name = entry.user.name
tw_name = entry.twitter_username
if name != tw_name
if nickname == auth.name
name = self_label
end
name += "(#{tw_name})"
return link_to(h(name), link_user(nickname))
end
end
end
super(entry.user)
end
def comment_icon(comment = nil)
if comment
by_self = comment.by_user(auth.name)
label = "##{comment.index}"
else
by_self = true
label = nil
end
by_self ? icon_tag(:comment, label) : icon_tag(:friend_comment, label)
end
def comment(comment)
fold, str, links = escape_text(comment.body, ctx.fold ? setting.text_folding_size : nil)
comment.view_links = links
if fold
str += link_to(inline_icon_tag(:more), link_show(comment.entry.id))
end
str
end
def inline_comment(comment)
if comment.posted_with_entry?
comment(comment)
else
comment(comment) + ' ' + comment_date(comment, true)
end
end
def search_form(opt = {})
str = ''
str += hidden_field_tag('user', ctx.user) if ctx.user
str += hidden_field_tag('list', ctx.list) if ctx.list
str += hidden_field_tag('room', ctx.room_for) if ctx.room_for
str += hidden_field_tag('friends', ctx.friends) if ctx.friends
str += hidden_field_tag('service', ctx.service) if ctx.service
if str.empty? and ctx.query.nil?
str += hidden_field_tag('friends', 'me')
end
if opt[:compact]
str += text_field_tag('query', ctx.query, :size => 6, :placeholder => 'search')
else
str += text_field_tag('query', ctx.query, :placeholder => 'search') + submit_tag('search')
end
str
end
def search_drilldown_links
opt = search_opt
links = []
likes = opt[:likes].to_i + 1
links << menu_link(h("[likes >= #{likes}]"), opt.merge(:likes => likes))
comments = opt[:comments].to_i + 1
links << menu_link(h("[comments >= #{comments}]"), opt.merge(:comments => comments))
mine_opt = opt.merge(:user => 'me')
mine_opt.delete(:friends)
links << menu_link(h('[mine]'), mine_opt) {
opt[:user] != 'me'
}
links << menu_link(h('[FriendFeed entry]'), opt.merge(:service => 'internal')) {
opt[:service] != 'internal'
}
h('drill down on: ') + links.join(' ')
end
def geo_markers_link(threads)
found = []
threads.each do |t|
t.entries.each do |e|
found << e if e.geo
end
end
if found.size > 1
google_maps_markers_link(found[0, 26])
end
end
def post_entry_form
str = ''
str += hidden_field_tag('room', ctx.room_for) + h(room_name(ctx.room_for)) + ': ' if ctx.room_for
str += text_field_tag('body', nil, :placeholder => 'post') + submit_tag('post')
str
end
def post_comment_form(entry)
if setting.twitter_comment_hack and entry.service.twitter? and user_status(entry.user_id) == 'public'
default = entry.twitter_username
unless default.empty?
default = "@#{default} "
end
end
text_field_tag('body', default, :placeholder => 'comment') + submit_tag('post')
end
def edit_comment_form(comment)
default = comment.body
text_field_tag('body', default) + submit_tag('post')
end
def fold_link(entry)
msg = " (#{entry.fold_entries} related entries)"
link_to(icon_tag(:more), list_opt(ctx.link_opt(:start => ctx.start, :num => ctx.num, :fold => 'no'))) + h(msg)
end
def fold_comment_link(fold)
msg = " (#{fold.fold_entries} more comments)"
link_to(icon_tag(:more), link_show(fold.entry_id)) + h(msg)
end
def write_new_link
link_to(icon_tag(:write), link_action('new', :room => u(ctx.room_for)))
end
def search_link
link_to(icon_tag(:search), search_opt(link_action('search')))
end
def service_links(user)
services = user_services(user)
map = services.inject({}) { |r, e|
r[e.id] = e.name
r
}
links_if_exists("#{map.size} services: ", map.to_a.sort_by { |k, v| k }) { |id, name|
label = "[#{name}]"
if ctx.service == id
h(label)
else
link_to(h(label), list_opt(link_user(user, :service => u(id))))
end
}
end
def list_links
lists = user_lists(auth.name)
links_if_exists('Lists: ', lists) { |e|
label = "[#{e.name}]"
if ctx.list == e.nickname
h(label)
else
link_to(h(label), link_list(:list => u(e.nickname)))
end
}
end
def zoom_select_tag(varname, default)
candidates = (0..19).map { |e| [e, e] }
select_tag(varname, options_for_select(candidates, default))
end
def room_select_tag(varname, default)
user = auth.name
rooms = user_rooms(user)
candidates = rooms.map { |e| [e.name, e.nickname] }
candidates.unshift([nil, nil])
select_tag(varname, options_for_select(candidates, default))
end
def likes_select_tag(varname, default)
candidates = [1, 2, 3, 4, 5, 10].map { |e| [e, e] }
candidates.unshift([nil, nil])
select_tag(varname, options_for_select(candidates, default))
end
def room_links(user)
rooms = user_rooms(user)
links_if_exists('Groups: ', rooms) { |e|
label = "[#{e.name}]"
if e.nickname == ctx.room_for and !ctx.service and !ctx.label
h(label)
else
link_to(h(label), link_list(:room => u(e.nickname)))
end
}
end
def user_links(user)
max = F2P::Config.max_friend_list_num
users = user_subscriptions(user)
users = users.find_all { |e| e.nickname and e.nickname != auth.name }
links_if_exists("#{users.size} subscriptions: ", users, max) { |e|
label = "[#{e.name}]"
link_to(h(label), link_user(e.nickname))
}
end
def imaginary_user_links(user)
max = F2P::Config.max_friend_list_num
users = user_subscriptions(user)
users = users.find_all { |e| !e.nickname }
links_if_exists("#{users.size} imaginary friends: ", users, max) { |e|
label = "<#{e.name}>"
link_to(h(label), link_user(e.id))
}
end
def member_links(room)
max = F2P::Config.max_friend_list_num
members = room_members(room)
me, rest = members.partition { |e| e.nickname == auth.name }
members = me + rest
links_if_exists("(#{members.size} members) ", members, max) { |e|
label = "[#{e.name}]"
if e.nickname
link_to(h(label), link_user(e.nickname))
end
}
end
def list_user_links(list)
max = F2P::Config.max_friend_list_num
users = list_users(list)
links_if_exists("(#{users.size} users) ", users, max) { |e|
label = "[#{e.name}]"
if e.nickname
link_to(h(label), link_user(e.nickname))
end
}
end
def list_room_links(list)
rooms = list_rooms(list)
links_if_exists("(#{rooms.size} groups) ", rooms) { |e|
label = "[#{e.name}]"
if e.nickname == ctx.room_for and !ctx.service and !ctx.label
h(label)
else
link_to(h(label), link_list(:room => u(e.nickname)))
end
}
end
def links_if_exists(label, enum, max = nil, &block)
if max and enum.size > max + 1
ary = enum[0, max].collect { |v| yield(v) }
ary << "... #{enum.size - max} more"
else
ary = enum.collect { |v| yield(v) }
end
str = ary.join(' ')
str = h(label) + str unless str.empty?
str
end
def page_links(opt = {})
no_page = ctx.start.nil?
start = ctx.start || 0
num = ctx.num || 0
links = []
if opt[:for_top]
links << menu_link(menu_icon(:bottom, '8'), '#bottom', accesskey('8'))
end
if opt[:for_bottom]
links << menu_link(menu_icon(:top, '2'), '#top', accesskey('2'))
end
if ctx.list?
links << menu_link(menu_label('<', '4', true), list_opt(ctx.link_opt(:start => start - num, :num => num, :direction => 'rewind')), accesskey('4')) {
!no_page and start - num >= 0
}
end
links << menu_link(menu_label('inbox', '0'), link_action('inbox'), accesskey('0'))
if ctx.list? and threads = opt[:threads]
if entry = find_show_entry(threads)
links << menu_link(menu_label('next', '1'), link_show(entry.id), accesskey('1'))
end
end
if ctx.inbox
links << menu_link(menu_label('all', '7'), link_list(), accesskey('7'))
end
pin_label = 'pin'
if threads = opt[:threads]
if threads.pins and threads.pins > 0
pin_label += "(#{threads.pins})"
end
end
links << menu_link(menu_label(pin_label, '9'), link_list(:label => u('pin')), accesskey('9'))
if ctx.list?
links << menu_link(menu_label('>', '6'), list_opt(ctx.link_opt(:start => start + num, :num => num)), accesskey('6')) { !no_page }
if threads = opt[:threads] and opt[:for_top]
links << list_range_notation(threads)
end
end
if ctx.inbox and opt[:for_bottom]
links << archive_button
end
links.join(' ')
end
def find_show_entry(threads)
if thread = threads.first
thread.root
end
end
def list_range_notation(threads)
if threads.from_modified and threads.to_modified
from = ago(threads.from_modified)
if ctx.start == 0
h("(#{from} ago ~ now)")
else
to = ago(threads.to_modified)