Relative time perl, emojis and attachment funcs

FossilOrigin-Name: 7a290aefc9a21f9789cb097ff99c1423fbf67a399f916ab2232e6eab7e83b460
This commit is contained in:
nekobit 2022-07-30 03:51:30 +00:00
parent e19f1f854e
commit d46209f0ee
7 changed files with 91 additions and 3 deletions

View file

@ -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,
);

19
perl/string_helpers.pm Normal file
View file

@ -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';
}

View file

@ -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;

View file

@ -21,6 +21,7 @@
#include <mastodont.h>
#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

View file

@ -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;
}

View file

@ -21,6 +21,7 @@
#include <stddef.h>
#include <mastodont.h>
#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

View file

@ -71,8 +71,6 @@
<span class="count">[% status.replies_count %]</span>
[% END %]
</label>
</td>
<td>
<form action="$prefix/status/[% status.id %]/interact" method="post">
@ -124,7 +122,9 @@
</a>
</td>
<td>
<a href="#[% status.id %]" class="time">[% status.created_at %]</a>
<a href="#[% status.id %]" class="time">
[%- rel_to_str(status.created_at) -%]
</a>
</td>
</tr>
</table>