LJSUP-17669: Login.bml form refactoring
[livejournal.git] / cgi-bin / LJ / Event / Befriended.pm
blob6160780551f70cc978d13fe3a670ec35420e0834
1 package LJ::Event::Befriended;
2 use strict;
3 use Scalar::Util qw(blessed);
4 use Carp qw(croak);
5 use base 'LJ::Event';
7 sub new {
8 my ($class, $u, $fromu) = @_;
10 my $uid = LJ::want_userid($u); ## friendid
11 my $fromuid = LJ::want_userid($fromu);
12 return $class->SUPER::new($uid, $fromuid);
15 sub is_common { 0 }
17 my @_ml_strings_en = (
18 'esn.public', # 'public',
19 'esn.befriended.subject', # '[[who]] added you as a friend!',
20 'esn.add_friend', # '[[openlink]]Add [[journal]] to your Friends list[[closelink]]',
21 'esn.read_journal', # '[[openlink]]Read [[postername]]\'s journal[[closelink]]',
22 'esn.view_profile', # '[[openlink]]View [[postername]]\'s profile[[closelink]]',
23 'esn.edit_friends', # '[[openlink]]Edit Friends[[closelink]]',
24 'esn.edit_groups', # '[[openlink]]Edit Friends groups[[closelink]]',
25 'esn.befriended.alert', # '[[who]] has added you as a friend.',
26 'esn.befriended.email_text', # 'Hi [[user]],
28 #[[poster]] has added you to their Friends list. They will now be able to read your[[entries]] entries on their Friends page.
30 #You can:',
31 'esn.befriended.openid_email_text', # 'Hi [[user]],
33 #[[poster]] has added you to their Friends list.
35 #You can:',
37 'esn.befriended.params.body', # [[user]] has added you as a friend.
38 'esn.befriended.params.subject', # Someone has addded you as friend.
39 'esn.befriended.actions.add.friend', # Add Friend
40 'esn.befriended.actions.view.profile', # View Profile
44 sub as_email_subject {
45 my ($self, $u) = @_;
46 return LJ::Lang::get_text($u->prop('browselang'), 'esn.befriended.subject', undef, { who => $self->friend->display_username } );
49 sub _as_email {
50 my ($self, $u, $is_html) = @_;
52 my $lang = $u->prop('browselang');
53 my $user = $is_html ? ($u->ljuser_display) : ($u->display_username);
54 my $poster = $is_html ? ($self->friend->ljuser_display) : ($self->friend->display_username);
55 my $postername = $self->friend->user;
56 my $journal_url = $self->friend->journal_base;
57 my $journal_profile = $self->friend->profile_url;
59 # Precache text lines
60 LJ::Lang::get_text_multi($lang, undef, \@_ml_strings_en);
62 my $entries = LJ::is_friend($u, $self->friend) ? "" : " " . LJ::Lang::get_text($lang, 'esn.public', undef);
63 my $is_open_identity = $self->friend->is_identity;
65 my $vars = {
66 who => $self->friend->display_username,
67 poster => $poster,
68 postername => $poster,
69 journal => $poster,
70 user => $user,
71 entries => $entries,
74 my $email_body_key = 'esn.befriended.' .
75 ($u->is_identity ? 'openid_' : '' ) . 'email_text';
77 return LJ::Lang::get_text($lang, $email_body_key, undef, $vars) .
78 $self->format_options($is_html, $lang, $vars,
80 'esn.add_friend' => [ LJ::is_friend($u, $self->friend) ? 0 : 1,
81 # Why not $self->friend->addfriend_url ?
82 "$LJ::SITEROOT/friends/add.bml?user=$postername" ],
83 'esn.read_journal' => [ $is_open_identity ? 0 : 2,
84 $journal_url ],
85 'esn.view_profile' => [ 3, $journal_profile ],
86 'esn.edit_friends' => [ 4, "$LJ::SITEROOT/friends/edit.bml" ],
87 'esn.edit_groups' => [ 5, "$LJ::SITEROOT/friends/editgroups.bml" ],
92 sub as_email_string {
93 my ($self, $u) = @_;
94 return _as_email($self, $u, 0);
97 sub as_email_html {
98 my ($self, $u) = @_;
99 return _as_email($self, $u, 1);
102 sub friend {
103 my $self = shift;
104 return LJ::load_userid($self->arg1);
107 sub as_html {
108 my $self = shift;
109 return sprintf("%s has added you as a friend.",
110 $self->friend->ljuser_display);
113 sub as_html_actions {
114 my ($self) = @_;
116 my $u = $self->u;
117 my $friend = $self->friend;
118 my $ret .= "<div class='actions'>";
119 $ret .= $u->is_friend($friend)
120 ? " <a href='" . $friend->profile_url . "'>View Profile</a>"
121 : " <a href='" . $friend->addfriend_url . "'>Add Friend</a>";
122 $ret .= "</div>";
124 return $ret;
127 sub as_string {
128 my $self = shift;
129 return sprintf("%s has added you as a friend.",
130 $self->friend->{user});
133 sub as_sms {
134 my ($self, $u, $opt) = @_;
135 my $lang = $u->prop('browselang') || $LJ::DEFAULT_LANG;
137 my $tinyurl = "http://m.livejournal.com/read/user/"
138 . $self->friend->user;
139 my $mparms = $opt->{mobile_url_extra_params};
140 $tinyurl .= '?' . join('&', map {$_ . '=' . $mparms->{$_}} keys %$mparms) if $mparms;
141 $tinyurl = LJ::Client::BitLy->shorten($tinyurl);
142 undef $tinyurl if $tinyurl =~ /^500/;
144 # [[friend]] has added you to their friends list. Reply with ADD [[friend]] to add them [[disclaimer]]
145 return LJ::Lang::get_text($lang, 'notification.sms.befriended', undef, {
146 friend => $self->friend->user,
147 disclaimer => $LJ::SMS_DISCLAIMER,
148 mobile_url => $tinyurl,
152 sub as_alert {
153 my $self = shift;
154 my $u = shift;
155 my $friend = $self->friend;
156 return '' unless $friend;
157 return LJ::Lang::get_text($u->prop('browselang'),
158 'esn.befriended.alert', undef, { who => $friend->ljuser_display() });
161 sub subscription_as_html {
162 my ($class, $subscr) = @_;
163 my $journal = $subscr->journal or croak "No user";
164 my $journal_is_owner = LJ::u_equals($journal, $subscr->owner);
166 if ($journal_is_owner) {
167 return LJ::Lang::ml('event.befriended.me'); # "Someone adds me as a friend";
168 } else {
169 my $user = $journal->ljuser_display;
170 return LJ::Lang::ml('event.befriended.user', { user => $user } ); # "Someone adds $user as a friend";
174 sub content {
175 my ($self, $target) = @_;
176 return $self->as_html_actions;
179 sub tmpl_params {
180 my ($self, $remote) = @_;
182 my $u = $self->u;
183 my $friend = $self->friend;
185 my $lang = $remote && $remote->prop('browselang') || $LJ::DEFAULT_LANG;
187 my $actions = [];
189 if ($u->is_friend($friend)) {
190 push @$actions, {
191 action_url => $friend->profile_url,
192 action => LJ::Lang::get_text($lang, "esn.befriended.actions.view.profile"),
194 } else {
195 push @$actions, {
196 action_url => $friend->addfriend_url,
197 action => LJ::Lang::get_text($lang, "esn.befriended.actions.add.friend"),
201 return {
202 body => LJ::Lang::get_text($lang, "esn.befriended.params.body", undef, { user => $friend->ljuser_display } ),
203 userpic => $friend->userpic ? $friend->userpic->url : '',
204 subject => LJ::Lang::get_text($lang, "esn.befriended.params.subject"),
205 actions => $actions,
209 sub available_for_user {
210 my ($self, $u) = @_;
212 return $self->userid != $u->id ? 0 : 1;
215 sub is_subscription_visible_to {
216 my ($self, $u) = @_;
218 return $self->userid != $u->id ? 0 : 1;
221 sub is_tracking { 0 }
223 sub as_push {
224 my $self = shift;
225 my $u = shift;
226 my $lang = shift;
228 return LJ::Lang::get_text($lang, "esn.push.notification.befriended", 1, {
229 user => $self->friend->user,
233 sub as_push_payload {
234 my $self = shift;
236 return { 't' => 7,
237 'j' => $self->friend->user,
241 sub update_events_counter {
242 my $self = shift;
244 my $u = $self->u;
245 return unless $u;
247 my $etypeid = $self->etypeid;
248 return unless $etypeid;
250 my $friend = $self->friend;
251 return unless $friend;
253 my $friendid = $friend->userid;
254 return unless $friendid;
256 LJ::Widget::HomePage::UpdatesForUser->add_event($u, pack("nnN", int(rand(2**16)), $etypeid, $friendid));