diff --git a/perl/status.pm b/perl/status.pm index e205f5d..a07bec6 100644 --- a/perl/status.pm +++ b/perl/status.pm @@ -1,6 +1,7 @@ package status; use strict; use warnings; +use string_helpers qw( reltime_to_str ); use icons qw( get_icon visibility_to_icon ); use Exporter 'import'; @@ -17,6 +18,7 @@ sub status ssn => $ssn, status => $status, icon => \&get_icon, + rel_to_str => \&reltime_to_str, vis_to_icon => \&visibility_to_icon, ); diff --git a/perl/string_helpers.pm b/perl/string_helpers.pm new file mode 100644 index 0000000..8dff450 --- /dev/null +++ b/perl/string_helpers.pm @@ -0,0 +1,19 @@ +package string_helpers; +use strict; +use warnings; + +use Exporter 'import'; + +our @EXPORT = qw( &reltime_to_str ); + +sub reltime_to_str +{ + my $since = time() - $_[0]; + + return $since . 's' if $since < 60; + return int($since / 60) . 'm' if $since < 60 * 60; + return int($since / (60 * 60)) . 'h' if $since < 60 * 60 * 24; + return int($since / (60 * 60 * 24)) . 'd' if $since < 60 * 60 * 24 * 31; + return int($since / (60 * 60 * 24 * 31)) . 'mon' if $since < 60 * 60 * 24 * 365; + return int($since / (60 * 60 * 24 * 365)) . 'yr'; +} diff --git a/src/attachments.c b/src/attachments.c index 6b7ce2d..1ad97e3 100644 --- a/src/attachments.c +++ b/src/attachments.c @@ -197,6 +197,35 @@ char* construct_attachments(struct session* ssn, return att_view; } +HV* perlify_attachment(struct mstdnt_attachment* const attachment) +{ + if (!attachments) return NULL; + HV* attach_hv = newHV(); + hvstores_str(attach_hv, "id", attachment->id); + hvstores_int(attach_hv, "type", attachment->type) + hvstores_str(attach_hv, "url", attachment->url); + hvstores_str(attach_hv, "preview_url", attachment->preview_url); + hvstores_str(attach_hv, "remote_url", attachment->remote_url); + hvstores_str(attach_hv, "hash", attachment->hash); + hvstores_str(attach_hv, "description", attachment->description); + hvstores_str(attach_hv, "blurhash", attachment->blurhash); + return attach_hv; +} + +AV* perlify_attachments(struct mstdnt_attachment* const attachments, size_t len) +{ + if (!(attachments && len)) return NULL; + AV* av = newAV(); + av_extend(av, len-1); + + for (int i = 0; i < len; ++i) + { + av_store(av, i, newRV_inc((SV*)perlify_attachment(attachments + i))); + } + + return av; +} + void api_attachment_create(PATH_ARGS) { struct mstdnt_storage *att_storage = NULL; diff --git a/src/attachments.h b/src/attachments.h index 9e54e2c..21e5100 100644 --- a/src/attachments.h +++ b/src/attachments.h @@ -21,6 +21,7 @@ #include #include "path.h" #include "session.h" +#include "global_perl.h" #define FILES_READY(ssn) (ssn->post.files.type.f.array_size && \ ssn->post.files.type.f.content && \ @@ -37,4 +38,8 @@ char* construct_attachment(struct session* ssn, mstdnt_bool sensitive, struct ms char* construct_attachments(struct session* ssn, mstdnt_bool sensitive, struct mstdnt_attachment* atts, size_t atts_len, size_t* str_size); void api_attachment_create(PATH_ARGS); +// Perl +HV* perlify_attachment(struct mstdnt_attachment* const attachment); +AV* perlify_attachments(struct mstdnt_attachment* const attachments, size_t len); + #endif // ATTACHMENTS_H diff --git a/src/emoji.c b/src/emoji.c index f26ce37..e60a9ad 100644 --- a/src/emoji.c +++ b/src/emoji.c @@ -163,3 +163,31 @@ char* construct_emoji_picker(char* status_id, size_t* size) free(emojis[i]); return emoji_picker_html; } + +HV* perlify_emoji(struct mstdnt_emoji* const emoji); +{ + if (!emoji) return NULL; + HV* emoji_hv = newHV(); + hvstores_str(emoji_hv, "shortcode", attachment->id); + hvstores_str(emoji_hv, "url", attachment->url); + hvstores_str(emoji_hv, "static_url", attachment->preview_url); + hvstores_str(emoji_hv, "visible_in_picker", attachment->visible_in_picker); + hvstores_str(emoji_hv, "hash", attachment->hash); + hvstores_str(emoji_hv, "description", attachment->description); + hvstores_str(emoji_hv, "blurhash", attachment->blurhash); + return emoji_hv; +} + +AV* perlify_emojis(struct mstdnt_emoji* const emos, size_t len) +{ + if (!(emos && len)) return NULL; + AV* av = newAV(); + av_extend(av, len-1); + + for (int i = 0; i < len; ++i) + { + av_store(av, i, newRV_inc((SV*)perlify_emoji(emos + i))); + } + + return av; +} diff --git a/src/emoji.h b/src/emoji.h index 3968009..67b624b 100644 --- a/src/emoji.h +++ b/src/emoji.h @@ -21,6 +21,7 @@ #include #include #include "emoji_codes.h" +#include "global_perl.h" #define EMOJI_FACTOR_NUM 32 @@ -36,4 +37,8 @@ char* construct_emoji(struct emoji_info* emoji, char* status_id, size_t* size); void content_emoji_picker(PATH_ARGS); char* construct_emoji_picker(char* status_id, size_t* size); +// Perl +HV* perlify_emoji(struct mstdnt_emoji* const emoji); +AV* perlify_emojis(struct mstdnt_emoji* const emos, size_t len); + #endif // EMOJI_H diff --git a/templates/status.tt b/templates/status.tt index 3965d88..1c57a5c 100644 --- a/templates/status.tt +++ b/templates/status.tt @@ -71,8 +71,6 @@ [% status.replies_count %] [% END %] - -
@@ -124,7 +122,9 @@ - [% status.created_at %] + + [%- rel_to_str(status.created_at) -%] +