/
Entry.pm
2447 lines (2006 loc) · 76.9 KB
/
Entry.pm
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
# This code was forked from the LiveJournal project owned and operated
# by Live Journal, Inc. The code has been modified and expanded by
# Dreamwidth Studios, LLC. These files were originally licensed under
# the terms of the license supplied by Live Journal, Inc, which can
# currently be found at:
#
# http://code.livejournal.org/trac/livejournal/browser/trunk/LICENSE-LiveJournal.txt
#
# In accordance with the original license, this code and all its
# modifications are provided under the GNU General Public License.
# A copy of that license can be found in the LICENSE file included as
# part of this distribution.
#
# LiveJournal entry object.
#
# Just framing right now, not much to see here!
#
package LJ::Entry;
use strict;
our $AUTOLOAD;
use Carp qw/ croak confess /;
=head1 NAME
LJ::Entry
=head1 CLASS METHODS
=cut
# internal fields:
#
# u: object, always present
# anum: lazily loaded, either by ctor or _loaded_row
# ditemid: lazily loaded
# jitemid: always present
# props: hashref of props, loaded if _loaded_props
# subject: text of subject, loaded if _loaded_text
# event: text of log event, loaded if _loaded_text
# subject_orig: text of subject without transcoding, present if unknown8bit
# event_orig: text of log event without transcoding, present if unknown8bit
# eventtime: mysql datetime of event, loaded if _loaded_row
# logtime: mysql datetime of event, loaded if _loaded_row
# security: "public", "private", "usemask", loaded if _loaded_row
# allowmask: if _loaded_row
# posterid: if _loaded_row
# comments: arrayref of comment objects on this entry
# talkdata: hash of raw comment data for this entry
# userpic
# _loaded_text: loaded subject/text
# _loaded_row: loaded log2 row
# _loaded_props: loaded props
# _loaded_comments: loaded comments
# _loaded_talkdata: loaded talkdata
my %singletons = (); # journalid->jitemid->singleton
sub reset_singletons {
%singletons = ();
}
# <LJFUNC>
# name: LJ::Entry::new
# class: entry
# des: Gets a journal entry.
# args: uuserid, opts
# des-uuserid: A user id or user object ($u ) to load the entry for.
# des-opts: Hash of optional keypairs.
# 'jitemid' => a journal itemid (no anum)
# 'ditemid' => display itemid (a jitemid << 8 + anum)
# 'anum' => the id passed was an ditemid, use the anum
# to create a proper jitemid.
# returns: A new LJ::Entry object. undef on failure.
# </LJFUNC>
sub new
{
my $class = shift;
my $self = bless {};
my $uuserid = shift;
my $n_arg = scalar @_;
croak("wrong number of arguments")
unless $n_arg && ($n_arg % 2 == 0);
my %opts = @_;
croak("can't supply both anum and ditemid")
if defined $opts{anum} && defined $opts{ditemid};
croak("can't supply both itemid and ditemid")
if defined $opts{ditemid} && defined $opts{jitemid};
# FIXME: don't store $u in here, or at least call LJ::load_userids() on all singletons
# if LJ::want_user() would have been called
$self->{u} = LJ::want_user($uuserid) or croak("invalid user/userid parameter: $uuserid");
$self->{anum} = delete $opts{anum};
$self->{ditemid} = delete $opts{ditemid};
$self->{jitemid} = delete $opts{jitemid};
# make arguments numeric
for my $f (qw(ditemid jitemid anum)) {
$self->{$f} = int($self->{$f}) if defined $self->{$f};
}
croak("need to supply either a jitemid or ditemid")
unless defined $self->{ditemid} || defined $self->{jitemid};
croak("Unknown parameters: " . join(", ", keys %opts))
if %opts;
if ($self->{ditemid}) {
$self->{_untrusted_anum} = $self->{ditemid} & 255;
$self->{jitemid} = $self->{ditemid} >> 8;
}
# do we have a singleton for this entry?
{
my $journalid = $self->{u}->{userid};
my $jitemid = $self->{jitemid};
$singletons{$journalid} ||= {};
return $singletons{$journalid}->{$jitemid}
if $singletons{$journalid}->{$jitemid};
# save the singleton if it doesn't exist
$singletons{$journalid}->{$jitemid} = $self;
}
return $self;
}
# sometimes item hashes don't have a journalid arg.
# in those cases call as ($u, $item) and the $u will
# be used
sub new_from_item_hash {
my ( $class, $arg1, $item ) = @_;
if (LJ::isu($arg1)) {
$item->{journalid} ||= $arg1->id;
} else {
$item = $arg1;
}
# some item hashes have 'jitemid', others have 'itemid'
$item->{jitemid} ||= $item->{itemid};
croak "invalid item hash"
unless $item && ref $item;
croak "no journalid in item hash"
unless $item->{journalid};
croak "no entry information in item hash"
unless $item->{ditemid} || ($item->{jitemid} && defined($item->{anum}));
my $entry;
# have a ditemid only? no problem.
if ($item->{ditemid}) {
$entry = LJ::Entry->new($item->{journalid},
ditemid => $item->{ditemid});
# jitemid/anum is okay too
} elsif ($item->{jitemid} && defined($item->{anum})) {
$entry = LJ::Entry->new($item->{journalid},
jitemid => $item->{jitemid},
anum => $item->{anum});
}
return $entry;
}
sub new_from_url {
my ($class, $url) = @_;
if ($url =~ m!(.+)/(\d+)\.html!) {
my $u = LJ::User->new_from_url($1) or return undef;
return LJ::Entry->new($u, ditemid => $2);
}
return undef;
}
sub new_from_row {
my ( $class, %row ) = @_;
my $journalu = LJ::load_userid($row{journalid});
my $self = $class->new($journalu, jitemid => $row{jitemid});
$self->absorb_row(%row);
return $self;
}
=head1 INSTANCE METHODS
=cut
# returns true if entry currently exists. (it's possible for a given
# $u, to make a fake jitemid and that'd be a valid skeleton LJ::Entry
# object, even though that jitemid hasn't been created yet, or was
# previously deleted)
sub valid {
my $self = $_[0];
__PACKAGE__->preload_rows([ $self ]) unless $self->{_loaded_row};
return $self->{_loaded_row};
}
sub jitemid {
my $self = $_[0];
return $self->{jitemid};
}
sub ditemid {
my $self = $_[0];
return $self->{ditemid} ||= (($self->{jitemid} << 8) + $self->anum);
}
sub reply_url {
my $self = $_[0];
return $self->url(mode => 'reply');
}
# returns permalink url
sub url {
my ( $self, %opts ) = @_;
my %style_args = %{ delete $opts{style_args} || {} };
my %args = %opts; # used later
@args{keys %style_args} = values %style_args;
my $u = $self->{u};
my $view = delete $opts{view};
my $anchor = delete $opts{anchor};
my $mode = delete $opts{mode};
croak "Unknown args passed to url: " . join(",", keys %opts)
if %opts;
my $override = LJ::Hooks::run_hook("entry_permalink_override", $self, %opts);
return $override if $override;
my $url = $u->journal_base . "/" . $self->ditemid . ".html";
delete $args{anchor};
if (%args) {
$url .= "?";
$url .= LJ::encode_url_string(\%args);
}
$url .= "#$anchor" if $anchor;
return $url;
}
# returns a url that will display the number of comments on the entry
# as an image
sub comment_image_url {
my $self = $_[0];
my $u = $self->{u};
return "$LJ::SITEROOT/tools/commentcount?user=" . $self->journal->user . "&ditemid=" . $self->ditemid;
}
# returns a pre-generated comment img tag using the comment_image_url
sub comment_imgtag {
my $self = $_[0];
my $alttext = LJ::Lang::ml('setting.xpost.option.footer.vars.comment_image.alt');
return '<img src="' . $self->comment_image_url . '" width="30" height="12" alt="' . $alttext . '" style="vertical-align: middle;"/>';
}
sub anum {
my $self = $_[0];
return $self->{anum} if defined $self->{anum};
__PACKAGE__->preload_rows([ $self ]) unless $self->{_loaded_row};
return $self->{anum} if defined $self->{anum};
croak("couldn't retrieve anum for entry");
}
# method:
# $entry->correct_anum
# $entry->correct_anum($given_anum)
# if no given anum, gets it from the provided ditemid to constructor
# Note: an anum parsed from the ditemid cannot be trusted which is what we're verifying here
sub correct_anum {
my ( $self, $given ) = @_;
$given = defined $given ? int( $given ) :
$self->{ditemid} ? $self->{_untrusted_anum} :
$self->{anum};
return 0 unless $self->valid;
return 0 unless defined $self->{anum} && defined $given;
return $self->{anum} == $given;
}
# returns LJ::User object for the poster of this entry
sub poster {
my $self = $_[0];
return LJ::load_userid($self->posterid);
}
sub posterid {
my $self = $_[0];
__PACKAGE__->preload_rows([ $self ]) unless $self->{_loaded_row};
return $self->{posterid};
}
sub journalid {
my $self = $_[0];
return $self->{u}{userid};
}
sub journal {
my $self = $_[0];
return $self->{u};
}
sub eventtime_mysql {
my $self = $_[0];
__PACKAGE__->preload_rows([ $self ]) unless $self->{_loaded_row};
return $self->{eventtime};
}
sub logtime_mysql {
my $self = $_[0];
__PACKAGE__->preload_rows([ $self ]) unless $self->{_loaded_row};
return $self->{logtime};
}
sub logtime_unix {
my $self = $_[0];
__PACKAGE__->preload_rows([ $self ]) unless $self->{_loaded_row};
return LJ::mysqldate_to_time($self->{logtime}, 1);
}
sub modtime_unix {
my $self = $_[0];
return $self->prop( "revtime" ) || $self->logtime_unix;
}
sub security {
my $self = $_[0];
__PACKAGE__->preload_rows([ $self ]) unless $self->{_loaded_row};
return $self->{security};
}
sub allowmask {
my $self = $_[0];
__PACKAGE__->preload_rows([ $self ]) unless $self->{_loaded_row};
return $self->{allowmask};
}
sub preload {
my ($class, $entlist) = @_;
$class->preload_rows($entlist);
$class->preload_props($entlist);
# TODO: $class->preload_text($entlist);
}
# class method:
sub preload_rows {
my ($class, $entlist) = @_;
foreach my $en (@$entlist) {
next if $en->{_loaded_row};
my $lg = LJ::get_log2_row($en->{u}, $en->{jitemid});
next unless $lg;
# absorb row into given LJ::Entry object
$en->absorb_row(%$lg);
}
}
sub absorb_row {
my ($self, %row) = @_;
$self->{$_} = $row{$_} foreach (qw(allowmask posterid eventtime logtime security anum));
$self->{_loaded_row} = 1;
}
# class method:
sub preload_props {
my ($class, $entlist) = @_;
foreach my $en (@$entlist) {
next if $en->{_loaded_props};
$en->_load_props;
}
}
# method for preloading props into all outstanding singletons that haven't already
# loaded properties.
sub preload_props_all {
foreach my $uid ( keys %singletons ) {
my $hr = $singletons{$uid};
my @load;
foreach my $jid ( keys %$hr ) {
next if $hr->{$jid}->{_loaded_props};
push @load, $jid;
}
my $props = {};
LJ::load_log_props2( $uid, \@load, $props );
foreach my $jid ( keys %$props ) {
$hr->{$jid}->{props} = $props->{$jid};
$hr->{$jid}->{_loaded_props} = 1;
}
}
}
# returns array of tags for this post
sub tags {
my $self = $_[0];
my $taginfo = LJ::Tags::get_logtags($self->journal, $self->jitemid);
return () unless $taginfo;
my $entry_taginfo = $taginfo->{$self->jitemid};
return () unless $entry_taginfo;
return values %$entry_taginfo;
}
# returns true if loaded, zero if not.
# also sets _loaded_text and subject and event.
sub _load_text {
my $self = $_[0];
return 1 if $self->{_loaded_text};
my $ret = LJ::get_logtext2($self->{'u'}, $self->{'jitemid'});
my $lt = $ret->{$self->{jitemid}};
return 0 unless $lt;
$self->{subject} = $lt->[0];
$self->{event} = $lt->[1];
if ($self->prop("unknown8bit")) {
# save the old ones away, so we can get back at them if we really need to
$self->{subject_orig} = $self->{subject};
$self->{event_orig} = $self->{event};
# FIXME: really convert all the props? what if we binary-pack some in the future?
LJ::item_toutf8($self->{u}, \$self->{'subject'}, \$self->{'event'}, $self->{props});
}
$self->{_loaded_text} = 1;
return 1;
}
sub prop {
my ($self, $prop) = @_;
$self->_load_props unless $self->{_loaded_props};
return $self->{props}{$prop};
}
sub props {
my ($self, $prop) = @_;
$self->_load_props unless $self->{_loaded_props};
return $self->{props} || {};
}
sub _load_props {
my $self = $_[0];
return 1 if $self->{_loaded_props};
my $props = {};
LJ::load_log_props2($self->{u}, [ $self->{jitemid} ], $props);
$self->{props} = $props->{ $self->{jitemid} };
$self->{_loaded_props} = 1;
return 1;
}
sub set_prop {
my ( $self, $prop, $val ) = @_;
LJ::set_logprop($self->journal, $self->jitemid, { $prop => $val });
$self->{props}{$prop} = $val;
return 1;
}
# called automatically on $event->comments
# returns the same data as LJ::get_talk_data, with the addition
# of 'subject' and 'event' keys.
sub _load_comments {
my $self = $_[0];
return 1 if $self->{_loaded_comments};
# need to load using talklib API
my $comment_ref = $self->{_loaded_talkdata} ? $self->{talkdata} : LJ::Talk::get_talk_data($self->journal, 'L', $self->jitemid);
die "unable to load comment data for entry"
unless ref $comment_ref;
my @comment_list;
my $u = $self->journal;
my $nodeid = $self->jitemid;
# instantiate LJ::Comment singletons and set them on our $self
foreach my $jtalkid ( keys %$comment_ref ) {
my $row = $comment_ref->{$jtalkid};
# at this point we have data for this comment loaded in memory
# -- instantiate an LJ::Comment object as a singleton and absorb
# that data into the object
my $comment = LJ::Comment->new($u, jtalkid => $jtalkid);
# add important info to row
$row->{nodetype} = "L";
$row->{nodeid} = $nodeid;
$comment->absorb_row(%$row);
push @comment_list, $comment;
}
$self->set_comment_list( @comment_list );
return $self;
}
sub comment_list {
my $self = $_[0];
$self->_load_comments unless $self->{_loaded_comments};
return @{$self->{comments} || []};
}
sub set_comment_list {
my ( $self, @args ) = @_;
$self->{comments} = \@args;
$self->{_loaded_comments} = 1;
return 1;
}
sub set_talkdata {
my ( $self, $talkdata ) = @_;
$self->{talkdata} = $talkdata;
$self->{_loaded_talkdata} = 1;
return 1;
}
sub reply_count {
my $self = $_[0];
my $rc = $self->prop('replycount');
return $rc if defined $rc;
return LJ::Talk::get_replycount($self->journal, $self->jitemid);
}
# returns "Leave a comment", "1 comment", "2 comments" etc
sub comment_text {
my $self = $_[0];
my $comments;
my $comment_count = $self->reply_count;
if ($comment_count) {
$comments = $comment_count == 1 ? "1 Comment" : "$comment_count Comments";
} else {
$comments = "Leave a comment";
}
return $comments;
}
# returns data hashref suitable for use in S2 CommentInfo function
sub comment_info {
my ( $self, %opts )= @_;
return unless %opts;
return unless exists $opts{u};
return unless exists $opts{remote};
return unless exists $opts{style_args};
my $u = $opts{u};
my $remote = $opts{remote};
my $style_args = $opts{style_args};
my $viewall = $opts{viewall};
my $journal = exists $opts{journal} ? $opts{journal} : $u;
my $permalink = $self->url;
my $comments_enabled = ( $viewall ||
( $journal->{opt_showtalklinks} eq "Y" && !$self->comments_disabled ) ) ? 1 : 0;
my $has_screened = ( $self->props->{hasscreened} && $remote && $journal
&& $remote->can_manage( $journal ) ) ? 1 : 0;
my $screenedcount = $has_screened ? LJ::Talk::get_screenedcount( $u, $self->jitemid ) : 0;
my $replycount = $comments_enabled ? $self->reply_count : 0;
my $nc = "";
$nc .= "nc=$replycount" if $replycount && $remote && $remote->{opt_nctalklinks};
return {
read_url => LJ::Talk::talkargs( $permalink, $nc, $style_args ),
post_url => LJ::Talk::talkargs( $permalink, "mode=reply", $style_args ),
permalink_url => LJ::Talk::talkargs( $permalink, $style_args ),
count => $replycount,
maxcomments => ( $replycount >= $u->count_maxcomments ) ? 1 : 0,
enabled => $comments_enabled,
comments_disabled_maintainer => $self->comments_disabled_maintainer,
screened => $has_screened,
screened_count => $screenedcount,
show_readlink => $comments_enabled && ( $replycount || $has_screened ),
show_postlink => $comments_enabled,
};
}
# used in comment notification email headers
sub email_messageid {
my $self = $_[0];
return "<" . join("-", "entry", $self->journal->id, $self->ditemid) . "\@$LJ::DOMAIN>";
}
sub atom_id {
my $self = $_[0];
my $u = $self->{u};
my $ditemid = $self->ditemid;
return $u->atomid . ":$ditemid";
}
# returns an XML::Atom::Entry object for a feed
# opts: synlevel ("full"), apilinks (bool)
sub atom_entry {
my ( $self, %opts ) = @_;
my $atom_entry = XML::Atom::Entry->new( Version => 1 );
my $u = $self->{u};
my $make_link = sub {
my ( $rel, $href, $type, $title ) = @_;
my $link = XML::Atom::Link->new( Version => 1 );
$link->rel( $rel );
$link->href( $href );
$link->title( $title ) if $title;
$link->type( $type ) if $type;
return $link;
};
$atom_entry->id( $self->atom_id );
$atom_entry->title( $self->subject_text );
$atom_entry->published( LJ::time_to_w3c( $self->logtime_unix, "Z" ) );
$atom_entry->updated( LJ::time_to_w3c( $self->modtime_unix, 'Z' ) );
my $author = XML::Atom::Person->new( Version => 1 );
$author->name( $self->poster->name_orig );
$atom_entry->author($author);
$atom_entry->add_link( $make_link->( "alternate", $self->url, "text/html" ) );
$atom_entry->add_link( $make_link->( "edit", $self->atom_url, "application/atom+xml", "Edit this post" ) )
if $opts{apilinks};
foreach my $tag ( $self->tags ) {
my $category = XML::Atom::Category->new( Version => 1 );
$category->term( $tag );
$atom_entry->add_category( $category );
}
my $syn_level = $opts{synlevel} || $u->prop( "opt_synlevel" ) || "full";
# if syndicating the complete entry
# -print a content tag
# elsif syndicating summaries
# -print a summary tag
# else (code omitted), we're syndicating title only
# -print neither (the title has already been printed)
# note: the $event was also emptied earlier, in make_feed
#
# a lack of a content element is allowed, as long
# as we maintain a proper 'alternate' link (above)
if ( $syn_level eq 'full' || $syn_level eq 'cut' ) {
$atom_entry->content( $self->event_html );
$atom_entry->content->type( "html" );
} elsif ( $syn_level eq 'summary' ) {
$atom_entry->summary( $self->event_summary );
$atom_entry->summary->type( "html" );
}
return $atom_entry;
}
sub atom_url {
my $self = $_[0];
return "" unless $self->journal;
return $self->journal->atom_base . "/entries/" . $self->jitemid;
}
# returns the entry as an XML Atom string, without the XML prologue
sub as_atom {
my $self = $_[0];
my $entry = $self->atom_entry;
my $xml = $entry->as_xml;
$xml =~ s!^<\?xml.+?>\s*!!s;
return $xml;
}
# raw utf8 text, with no HTML cleaning
sub subject_raw {
my $self = $_[0];
$self->_load_text unless $self->{_loaded_text};
return $self->{subject};
}
# raw text as user sent us, without transcoding while correcting for unknown8bit
sub subject_orig {
my $self = $_[0];
$self->_load_text unless $self->{_loaded_text};
return $self->{subject_orig} || $self->{subject};
}
# raw utf8 text, with no HTML cleaning
sub event_raw {
my $self = $_[0];
$self->_load_text unless $self->{_loaded_text};
return $self->{event};
}
# raw text as user sent us, without transcoding while correcting for unknown8bit
sub event_orig {
my $self = $_[0];
$self->_load_text unless $self->{_loaded_text};
return $self->{event_orig} || $self->{event};
}
sub subject_html {
my $self = $_[0];
$self->_load_text unless $self->{_loaded_text};
my $subject = $self->{subject};
LJ::CleanHTML::clean_subject( \$subject ) if $subject;
return $subject;
}
sub subject_text {
my $self = $_[0];
$self->_load_text unless $self->{_loaded_text};
my $subject = $self->{subject};
LJ::CleanHTML::clean_subject_all( \$subject ) if $subject;
return $subject;
}
# instance method. returns HTML-cleaned/formatted version of the event
# optional $opt may be:
# undef: loads the opt_preformatted key and uses that for formatting options
# 1: treats entry as preformatted (no breaks applied)
# 0: treats entry as normal (newlines convert to HTML breaks)
# hashref: passed to LJ::CleanHTML::clean_event verbatim
sub event_html
{
my ($self, $opts) = @_;
if (! defined $opts) {
$self->_load_props unless $self->{_loaded_props};
$opts = { preformatted => $self->{props}{opt_preformatted} };
} elsif (! ref $opts) {
$opts = { preformatted => $opts };
}
my $remote = LJ::get_remote();
my $suspend_msg = $self->should_show_suspend_msg_to($remote) ? 1 : 0;
$opts->{suspend_msg} = $suspend_msg;
$opts->{unsuspend_supportid} = $suspend_msg ? $self->prop("unsuspend_supportid") : 0;
$self->_load_text unless $self->{_loaded_text};
my $event = $self->{event};
LJ::CleanHTML::clean_event(\$event, $opts);
LJ::expand_embedded($self->{u}, $self->ditemid, LJ::User->remote, \$event, sandbox => $opts->{sandbox} );
return $event;
}
# like event_html, but trimmed to $char_max
sub event_html_summary {
my ($self, $char_max, $opts, $trunc_ref) = @_;
return LJ::html_trim($self->event_html($opts), $char_max, $trunc_ref);
}
sub event_text {
my $self = $_[0];
my $event = $self->event_raw;
LJ::CleanHTML::clean_event( \$event, { textonly => 1} ) if $event;
return $event;
}
# like event_html, but truncated for summary mode in rss/atom
sub event_summary {
my $self = $_[0];
my $url = $self->url;
my $readmore = "<b>(<a href=\"$url\">Read more ...</a>)</b>";
return LJ::Entry->summarize( $self->event_html, $readmore );
}
# class method for truncation
sub summarize {
my ( $class, $event, $readmore ) = @_;
return '' unless defined $event;
# assume the first paragraph is terminated by two <br> or a </p>
# valid XML tags should be handled, even though it makes an uglier regex
if ( $event =~ m!(.*?(?:(?:<br\s*/?>(?:</br\s*>)?\s*){2}|</p\s*>))!i ) {
# everything before the matched tag + the tag itself
# + a link to read more
$event = $1 . $readmore;
}
return $event;
}
sub comments_manageable_by {
my ($self, $remote) = @_;
return 0 unless $self->valid;
return 0 unless $remote;
my $u = $self->{u};
return $remote->userid == $self->posterid || $remote->can_manage( $u );
}
# instance method: returns bool, if remote user can edit this entry
# use this to determine whether to, e.g., show edit buttons or an edit form
# but don't use this when saving stuff to the database -- those need to pass through the protocol
# does not care about readonly status, just about permissions
sub editable_by
{
my ( $self, $remote ) = @_;
return 0 unless LJ::isu( $remote );
return 0 unless $self->visible_to( $remote );
# remote is editing their own entry
return 1 if $self->posterid == $remote->userid;
# editing an entry that's not your personal journal
return 1 if $self->journalid != $self->posterid && $remote->can_manage( $self->journal );
return 0;
}
# instance method: returns bool, if remote user can view this entry
sub visible_to
{
my ($self, $remote, $canview) = @_;
return 0 unless $self->valid;
my ($viewall, $viewsome) = (0, 0);
if ( LJ::isu( $remote ) && $canview ) {
$viewall = $remote->has_priv( 'canview', '*' );
$viewsome = $viewall || $remote->has_priv( 'canview', 'suspended' );
}
# can see anything with viewall
return 1 if $viewall;
# can't see anything unless the journal is visible
# unless you have viewsome. then, other restrictions apply
unless ( $viewsome ) {
return 0 if $self->journal->is_inactive;
# can't see anything by suspended users
return 0 if $self->poster->is_suspended;
# can't see suspended entries
return 0 if $self->is_suspended_for($remote);
}
# public is okay
return 1 if $self->{'security'} eq "public";
# must be logged in otherwise
return 0 unless $remote;
my $userid = int($self->{u}{userid});
my $remoteid = int($remote->{userid});
# owners can always see their own.
return 1 if $userid == $remoteid;
# should be 'usemask' or 'private' security from here out, otherwise
# assume it's something new and return 0
return 0 unless $self->{security} eq "usemask" || $self->{security} eq "private";
return 0 unless $remote->is_individual;
if ( $self->security eq "private" ) {
# other people can't read private on personal journals
return 0 if $self->journal->is_individual;
# but community administrators can read private entries on communities
return 1 if $self->journal->is_community && $remote->can_manage( $self->journal );
# private entry on a community; we're not allowed to see this
return 0;
}
if ( $self->security eq "usemask" ) {
# check if it's a community and they're a member
return 1 if $self->journal->is_community &&
$remote->member_of( $self->journal );
my $gmask = $self->journal->trustmask( $remote );
my $allowed = (int($gmask) & int($self->{'allowmask'}));
return $allowed ? 1 : 0; # no need to return matching mask
}
return 0;
}
# returns hashref of (kwid => tag) for tags on the entry
sub tag_map {
my $self = $_[0];
my $tags = LJ::Tags::get_logtags($self->{u}, $self->jitemid);
return {} unless $tags;
return $tags->{$self->jitemid} || {};
}
=head2 C<< $entry->userpic >>
Returns a LJ::Userpic object for this post, or undef.
If called in a list context, returns ( LJ::Userpic object, keyword )
See userpic_kw.
=cut
# FIXME: add a context option for friends page, and perhaps
# respect $remote's userpic viewing preferences (community shows poster
# vs community's picture)
sub userpic {
my $up = $_[0]->poster;
my $kw = $_[0]->userpic_kw;
my $pic = LJ::Userpic->new_from_keyword( $up, $kw ) || $up->userpic;
return wantarray ? ( $pic, $kw ) : $pic;
}
=head2 C<< $entry->userpic_kw >>
Returns the keyword to use for the entry.
If a keyword is specified, it uses that.
=cut
sub userpic_kw {
my $self = $_[0];
my $up = $self->poster;
my $key;
# try their entry-defined userpic keyword
if ( $up->userpic_have_mapid ) {
my $mapid = $self->prop('picture_mapid');
$key = $up->get_keyword_from_mapid( $mapid ) if $mapid;
} else {
$key = $self->prop('picture_keyword');
}
return $key;
}
# returns true if the user is allowed to share an entry via Tell a Friend
# $u is the logged-in user
# $item is a hash containing Entry info
sub can_tellafriend {
my ($entry, $u) = @_;
return 1 if $entry->security eq 'public';
return 0 if $entry->security eq 'private';
# friends only
return 0 unless $entry->journal->is_person;
return 0 unless $u && $u->equals( $entry->poster );
return 1;
}
# defined by the entry poster
sub adult_content {
my $self = $_[0];
return $self->prop('adult_content');
}
# defined by a community maintainer
sub adult_content_maintainer {
my $self = $_[0];
my $userLevel = $self->adult_content;
my $maintLevel = $self->prop( 'adult_content_maintainer' );
return undef unless $maintLevel;
return $maintLevel if $userLevel eq $maintLevel;
return $maintLevel if !$userLevel || $userLevel eq "none";
return $maintLevel if $userLevel eq "concepts" && $maintLevel eq "explicit";
return undef;
}
# defined by a community maintainer