2022-11-07 17:13:03 -06:00
# -*- coding: utf-8 -*-
import arrow
2022-11-13 22:17:28 -06:00
import languageHandler
2022-11-10 15:32:49 -06:00
from . import utils , templates
2022-11-07 17:13:03 -06:00
2022-12-23 13:58:10 -06:00
def compose_post ( post , db , relative_times , show_screen_names , safe = True ) :
2022-11-07 17:13:03 -06:00
if show_screen_names == False :
2022-11-16 13:28:45 -06:00
user = post . account . get ( " display_name " )
2022-11-08 15:46:16 -06:00
if user == " " :
2022-11-16 13:28:45 -06:00
user = post . account . get ( " username " )
2022-11-07 17:13:03 -06:00
else :
2022-11-16 13:28:45 -06:00
user = post . account . get ( " acct " )
original_date = arrow . get ( post . created_at )
2022-11-07 17:13:03 -06:00
if relative_times :
2022-11-13 22:17:28 -06:00
ts = original_date . humanize ( locale = languageHandler . curLang [ : 2 ] )
2022-11-07 17:13:03 -06:00
else :
2022-11-13 22:17:28 -06:00
ts = original_date . shift ( hours = db [ " utc_offset " ] ) . format ( _ ( " dddd, MMMM D, YYYY H:m " ) , locale = languageHandler . curLang [ : 2 ] )
2022-11-16 13:28:45 -06:00
if post . reblog != None :
2022-12-23 13:58:10 -06:00
text = _ ( " Boosted from @ {} : {} " ) . format ( post . reblog . account . acct , templates . process_text ( post . reblog , safe = safe ) )
2022-11-07 17:13:03 -06:00
else :
2022-12-23 13:58:10 -06:00
text = templates . process_text ( post , safe = safe )
2022-11-16 13:28:45 -06:00
source = post . get ( " application " , " " )
2022-11-08 08:59:09 -06:00
# "" means remote user, None for legacy apps so we should cover both sides.
if source != None and source != " " :
2022-11-07 17:13:03 -06:00
source = source . get ( " name " , " " )
2022-11-08 08:59:09 -06:00
else :
source = " "
2022-11-07 17:13:03 -06:00
return [ user + " , " , text , ts + " , " , source ]
2022-11-08 12:21:03 -06:00
2022-12-23 13:58:10 -06:00
def compose_user ( user , db , relative_times = True , show_screen_names = False , safe = False ) :
2022-11-08 12:21:03 -06:00
original_date = arrow . get ( user . created_at )
if relative_times :
2022-11-13 22:17:28 -06:00
ts = original_date . humanize ( locale = languageHandler . curLang [ : 2 ] )
2022-11-08 12:21:03 -06:00
else :
2022-11-13 22:17:28 -06:00
ts = original_date . shift ( hours = db [ " utc_offset " ] ) . format ( _ ( " dddd, MMMM D, YYYY H:m:s " ) , locale = languageHandler . curLang [ : 2 ] )
2022-11-08 15:46:16 -06:00
name = user . display_name
if name == " " :
name = user . get ( " username " )
2022-11-16 13:28:45 -06:00
return [ _ ( " %s (@ %s ). %s followers, %s following, %s posts. Joined %s " ) % ( name , user . acct , user . followers_count , user . following_count , user . statuses_count , ts ) ]
2022-11-12 11:20:16 -06:00
2022-12-23 13:58:10 -06:00
def compose_conversation ( conversation , db , relative_times , show_screen_names , safe = False ) :
2022-11-12 11:20:16 -06:00
users = [ ]
for account in conversation . accounts :
if account . display_name != " " :
users . append ( account . display_name )
else :
users . append ( account . username )
users = " , " . join ( users )
2022-11-16 13:28:45 -06:00
last_post = compose_post ( conversation . last_status , db , relative_times , show_screen_names )
text = _ ( " Last message from {} : {} " ) . format ( last_post [ 0 ] , last_post [ 1 ] )
return [ users , text , last_post [ - 2 ] , last_post [ - 1 ] ]
2022-12-14 12:09:14 -06:00
2022-12-23 13:58:10 -06:00
def compose_notification ( notification , db , relative_times , show_screen_names , safe = False ) :
2022-12-14 12:09:14 -06:00
if show_screen_names == False :
user = notification . account . get ( " display_name " )
if user == " " :
user = notification . account . get ( " username " )
else :
user = notification . account . get ( " acct " )
original_date = arrow . get ( notification . created_at )
if relative_times :
ts = original_date . humanize ( locale = languageHandler . curLang [ : 2 ] )
else :
ts = original_date . shift ( hours = db [ " utc_offset " ] ) . format ( _ ( " dddd, MMMM D, YYYY H:m " ) , locale = languageHandler . curLang [ : 2 ] )
text = " Unknown: %r " % ( notification )
2023-01-29 11:39:52 -06:00
if notification . type == " status " :
text = _ ( " {username} has posted: {status} " ) . format ( username = user , status = " , " . join ( compose_post ( notification . status , db , relative_times , show_screen_names , safe = safe ) ) )
elif notification . type == " mention " :
2022-12-23 13:58:10 -06:00
text = _ ( " {username} has mentionned you: {status} " ) . format ( username = user , status = " , " . join ( compose_post ( notification . status , db , relative_times , show_screen_names , safe = safe ) ) )
2022-12-14 12:09:14 -06:00
elif notification . type == " reblog " :
2022-12-23 13:58:10 -06:00
text = _ ( " {username} has boosted: {status} " ) . format ( username = user , status = " , " . join ( compose_post ( notification . status , db , relative_times , show_screen_names , safe = safe ) ) )
2022-12-14 12:09:14 -06:00
elif notification . type == " favourite " :
2022-12-23 13:58:10 -06:00
text = _ ( " {username} has added to favorites: {status} " ) . format ( username = user , status = " , " . join ( compose_post ( notification . status , db , relative_times , show_screen_names , safe = safe ) ) )
2022-12-14 12:09:14 -06:00
elif notification . type == " follow " :
text = _ ( " {username} has followed you. " ) . format ( username = user )
2023-02-06 02:38:18 -06:00
elif notification . type == " admin.sign_up " :
text = _ ( " {username} has joined the instance. " ) . format ( username = user )
2022-12-14 12:09:14 -06:00
elif notification . type == " poll " :
2022-12-23 13:58:10 -06:00
text = _ ( " A poll in which you have voted has expired: {status} " ) . format ( status = " , " . join ( compose_post ( notification . status , db , relative_times , show_screen_names , safe = safe ) ) )
2022-12-14 12:09:14 -06:00
elif notification . type == " follow_request " :
text = _ ( " {username} wants to follow you. " ) . format ( username = user )
return [ user , text , ts ]