diff options
| author | 2024-01-24 19:30:01 +0100 | |
|---|---|---|
| committer | 2024-01-24 19:30:01 +0100 | |
| commit | 5b3a2fbd88aad1cf6f108a8a4943c9f106d04deb (patch) | |
| tree | 78927892b931901aeaab4782bcf54210df427b24 | |
| parent | New function get_attachments() (still unused). (diff) | |
| download | snac2-5b3a2fbd88aad1cf6f108a8a4943c9f106d04deb.tar.gz snac2-5b3a2fbd88aad1cf6f108a8a4943c9f106d04deb.tar.xz snac2-5b3a2fbd88aad1cf6f108a8a4943c9f106d04deb.zip | |
Use get_attachments() in html_entry().
| -rw-r--r-- | activitypub.c | 4 | ||||
| -rw-r--r-- | html.c | 138 | ||||
| -rw-r--r-- | snac.h | 1 |
3 files changed, 21 insertions, 122 deletions
diff --git a/activitypub.c b/activitypub.c index 79b8350..e680e33 100644 --- a/activitypub.c +++ b/activitypub.c | |||
| @@ -191,7 +191,7 @@ xs_list *get_attachments(const xs_dict *msg) | |||
| 191 | xs_list *p; | 191 | xs_list *p; |
| 192 | 192 | ||
| 193 | /* try first the attachments list */ | 193 | /* try first the attachments list */ |
| 194 | if (!xs_is_null(p = xs_dict_get(msg, "attachments"))) { | 194 | if (!xs_is_null(p = xs_dict_get(msg, "attachment"))) { |
| 195 | xs *attach = NULL; | 195 | xs *attach = NULL; |
| 196 | xs_val *v; | 196 | xs_val *v; |
| 197 | 197 | ||
| @@ -262,7 +262,7 @@ xs_list *get_attachments(const xs_dict *msg) | |||
| 262 | if (xs_type(v) == XSTYPE_DICT) { | 262 | if (xs_type(v) == XSTYPE_DICT) { |
| 263 | char *mtype = xs_dict_get(v, "type"); | 263 | char *mtype = xs_dict_get(v, "type"); |
| 264 | 264 | ||
| 265 | if (xs_type(type) == XSTYPE_STRING && strcmp(type, "Link") == 0) { | 265 | if (xs_type(mtype) == XSTYPE_STRING && strcmp(mtype, "Link") == 0) { |
| 266 | mtype = xs_dict_get(v, "mediaType"); | 266 | mtype = xs_dict_get(v, "mediaType"); |
| 267 | xs_list *tag = xs_dict_get(v, "tag"); | 267 | xs_list *tag = xs_dict_get(v, "tag"); |
| 268 | 268 | ||
| @@ -1643,28 +1643,9 @@ xs_html *html_entry(snac *user, xs_dict *msg, int local, | |||
| 1643 | } | 1643 | } |
| 1644 | 1644 | ||
| 1645 | /** attachments **/ | 1645 | /** attachments **/ |
| 1646 | v = xs_dict_get(msg, "attachment"); | 1646 | xs *attach = get_attachments(msg); |
| 1647 | |||
| 1648 | if (!xs_is_null(v)) { /** attachments **/ | ||
| 1649 | xs *attach = NULL; | ||
| 1650 | |||
| 1651 | /* ensure it's a list */ | ||
| 1652 | if (xs_type(v) == XSTYPE_DICT) { | ||
| 1653 | attach = xs_list_new(); | ||
| 1654 | attach = xs_list_append(attach, v); | ||
| 1655 | } | ||
| 1656 | else | ||
| 1657 | if (xs_type(v) == XSTYPE_LIST) | ||
| 1658 | attach = xs_dup(v); | ||
| 1659 | else | ||
| 1660 | attach = xs_list_new(); | ||
| 1661 | |||
| 1662 | /* does the message have an image? */ | ||
| 1663 | if (xs_type(v = xs_dict_get(msg, "image")) == XSTYPE_DICT) { | ||
| 1664 | /* add it to the attachment list */ | ||
| 1665 | attach = xs_list_append(attach, v); | ||
| 1666 | } | ||
| 1667 | 1647 | ||
| 1648 | { | ||
| 1668 | /* make custom css for attachments easier */ | 1649 | /* make custom css for attachments easier */ |
| 1669 | xs_html *content_attachments = xs_html_tag("div", | 1650 | xs_html *content_attachments = xs_html_tag("div", |
| 1670 | xs_html_attr("class", "snac-content-attachments")); | 1651 | xs_html_attr("class", "snac-content-attachments")); |
| @@ -1675,152 +1656,69 @@ xs_html *html_entry(snac *user, xs_dict *msg, int local, | |||
| 1675 | xs_list *p = attach; | 1656 | xs_list *p = attach; |
| 1676 | 1657 | ||
| 1677 | while (xs_list_iter(&p, &v)) { | 1658 | while (xs_list_iter(&p, &v)) { |
| 1678 | char *t = xs_dict_get(v, "mediaType"); | 1659 | char *type = xs_dict_get(v, "type"); |
| 1679 | 1660 | char *href = xs_dict_get(v, "href"); | |
| 1680 | if (xs_is_null(t)) | ||
| 1681 | t = xs_dict_get(v, "type"); | ||
| 1682 | |||
| 1683 | if (xs_is_null(t)) | ||
| 1684 | continue; | ||
| 1685 | |||
| 1686 | char *url = xs_dict_get(v, "url"); | ||
| 1687 | if (xs_is_null(url)) | ||
| 1688 | url = xs_dict_get(v, "href"); | ||
| 1689 | if (xs_is_null(url)) | ||
| 1690 | continue; | ||
| 1691 | |||
| 1692 | /* infer MIME type from non-specific attachments */ | ||
| 1693 | if (xs_list_len(attach) < 2 && xs_match(t, "Link|Document")) { | ||
| 1694 | char *mt = (char *)xs_mime_by_ext(url); | ||
| 1695 | |||
| 1696 | if (xs_match(mt, "image/*|audio/*|video/*")) /* */ | ||
| 1697 | t = mt; | ||
| 1698 | } | ||
| 1699 | |||
| 1700 | char *name = xs_dict_get(v, "name"); | 1661 | char *name = xs_dict_get(v, "name"); |
| 1701 | if (xs_is_null(name)) | ||
| 1702 | name = xs_dict_get(msg, "name"); | ||
| 1703 | if (xs_is_null(name)) | ||
| 1704 | name = L("No description"); | ||
| 1705 | 1662 | ||
| 1706 | if (xs_startswith(t, "image/") || strcmp(t, "Image") == 0) { | 1663 | if (xs_startswith(type, "image/") || strcmp(type, "Image") == 0) { |
| 1707 | xs_html_add(content_attachments, | 1664 | xs_html_add(content_attachments, |
| 1708 | xs_html_tag("a", | 1665 | xs_html_tag("a", |
| 1709 | xs_html_attr("href", url), | 1666 | xs_html_attr("href", href), |
| 1710 | xs_html_attr("target", "_blank"), | 1667 | xs_html_attr("target", "_blank"), |
| 1711 | xs_html_sctag("img", | 1668 | xs_html_sctag("img", |
| 1712 | xs_html_attr("loading", "lazy"), | 1669 | xs_html_attr("loading", "lazy"), |
| 1713 | xs_html_attr("src", url), | 1670 | xs_html_attr("src", href), |
| 1714 | xs_html_attr("alt", name), | 1671 | xs_html_attr("alt", name), |
| 1715 | xs_html_attr("title", name)))); | 1672 | xs_html_attr("title", name)))); |
| 1716 | } | 1673 | } |
| 1717 | else | 1674 | else |
| 1718 | if (xs_startswith(t, "video/")) { | 1675 | if (xs_startswith(type, "video/")) { |
| 1719 | xs_html_add(content_attachments, | 1676 | xs_html_add(content_attachments, |
| 1720 | xs_html_tag("video", | 1677 | xs_html_tag("video", |
| 1721 | xs_html_attr("preload", "none"), | 1678 | xs_html_attr("preload", "none"), |
| 1722 | xs_html_attr("style", "width: 100%"), | 1679 | xs_html_attr("style", "width: 100%"), |
| 1723 | xs_html_attr("class", "snac-embedded-video"), | 1680 | xs_html_attr("class", "snac-embedded-video"), |
| 1724 | xs_html_attr("controls", NULL), | 1681 | xs_html_attr("controls", NULL), |
| 1725 | xs_html_attr("src", url), | 1682 | xs_html_attr("src", href), |
| 1726 | xs_html_text(L("Video")), | 1683 | xs_html_text(L("Video")), |
| 1727 | xs_html_text(": "), | 1684 | xs_html_text(": "), |
| 1728 | xs_html_tag("a", | 1685 | xs_html_tag("a", |
| 1729 | xs_html_attr("href", url), | 1686 | xs_html_attr("href", href), |
| 1730 | xs_html_text(name)))); | 1687 | xs_html_text(name)))); |
| 1731 | } | 1688 | } |
| 1732 | else | 1689 | else |
| 1733 | if (xs_startswith(t, "audio/")) { | 1690 | if (xs_startswith(type, "audio/")) { |
| 1734 | xs_html_add(content_attachments, | 1691 | xs_html_add(content_attachments, |
| 1735 | xs_html_tag("audio", | 1692 | xs_html_tag("audio", |
| 1736 | xs_html_attr("preload", "none"), | 1693 | xs_html_attr("preload", "none"), |
| 1737 | xs_html_attr("style", "width: 100%"), | 1694 | xs_html_attr("style", "width: 100%"), |
| 1738 | xs_html_attr("class", "snac-embedded-audio"), | 1695 | xs_html_attr("class", "snac-embedded-audio"), |
| 1739 | xs_html_attr("controls", NULL), | 1696 | xs_html_attr("controls", NULL), |
| 1740 | xs_html_attr("src", url), | 1697 | xs_html_attr("src", href), |
| 1741 | xs_html_text(L("Audio")), | 1698 | xs_html_text(L("Audio")), |
| 1742 | xs_html_text(": "), | 1699 | xs_html_text(": "), |
| 1743 | xs_html_tag("a", | 1700 | xs_html_tag("a", |
| 1744 | xs_html_attr("href", url), | 1701 | xs_html_attr("href", href), |
| 1745 | xs_html_text(name)))); | 1702 | xs_html_text(name)))); |
| 1746 | } | 1703 | } |
| 1747 | else | 1704 | else |
| 1748 | if (strcmp(t, "Link") == 0) { | 1705 | if (strcmp(type, "Link") == 0) { |
| 1749 | xs_html_add(content_attachments, | 1706 | xs_html_add(content_attachments, |
| 1750 | xs_html_tag("p", | 1707 | xs_html_tag("p", |
| 1751 | xs_html_tag("a", | 1708 | xs_html_tag("a", |
| 1752 | xs_html_attr("href", url), | 1709 | xs_html_attr("href", href), |
| 1753 | xs_html_text(url)))); | 1710 | xs_html_text(href)))); |
| 1754 | } | 1711 | } |
| 1755 | else { | 1712 | else { |
| 1756 | xs_html_add(content_attachments, | 1713 | xs_html_add(content_attachments, |
| 1757 | xs_html_tag("p", | 1714 | xs_html_tag("p", |
| 1758 | xs_html_tag("a", | 1715 | xs_html_tag("a", |
| 1759 | xs_html_attr("href", url), | 1716 | xs_html_attr("href", href), |
| 1760 | xs_html_text(L("Attachment")), | 1717 | xs_html_text(L("Attachment")), |
| 1761 | xs_html_text(": "), | 1718 | xs_html_text(": "), |
| 1762 | xs_html_text(url)))); | 1719 | xs_html_text(href)))); |
| 1763 | } | ||
| 1764 | } | ||
| 1765 | } | ||
| 1766 | |||
| 1767 | /** urls (attachments from Peertube) **/ | ||
| 1768 | v = xs_dict_get(msg, "url"); | ||
| 1769 | |||
| 1770 | if (xs_type(v) == XSTYPE_LIST) { | ||
| 1771 | xs_list *p = v; | ||
| 1772 | char *url = NULL; | ||
| 1773 | |||
| 1774 | while (url == NULL && xs_list_iter(&p, &v)) { | ||
| 1775 | if (xs_type(v) == XSTYPE_DICT) { | ||
| 1776 | char *type = xs_dict_get(v, "type"); | ||
| 1777 | |||
| 1778 | if (xs_type(type) == XSTYPE_STRING && strcmp(type, "Link") == 0) { | ||
| 1779 | char *mtype = xs_dict_get(v, "mediaType"); | ||
| 1780 | xs_list *tag = xs_dict_get(v, "tag"); | ||
| 1781 | |||
| 1782 | if (xs_type(mtype) == XSTYPE_STRING && | ||
| 1783 | strcmp(mtype, "application/x-mpegURL") == 0 && | ||
| 1784 | xs_type(tag) == XSTYPE_LIST) { | ||
| 1785 | /* now iterate the tag list, looking for a video URL */ | ||
| 1786 | xs_dict *d; | ||
| 1787 | |||
| 1788 | while (url == NULL && xs_list_iter(&tag, &d)) { | ||
| 1789 | if (xs_type(d) == XSTYPE_DICT) { | ||
| 1790 | if (xs_type(mtype = xs_dict_get(d, "mediaType")) == XSTYPE_STRING && | ||
| 1791 | xs_startswith(mtype, "video/")) { | ||
| 1792 | |||
| 1793 | /* this is probably it */ | ||
| 1794 | if (xs_type(url = xs_dict_get(d, "href")) != XSTYPE_STRING) | ||
| 1795 | url = NULL; | ||
| 1796 | } | ||
| 1797 | } | ||
| 1798 | } | ||
| 1799 | } | ||
| 1800 | } | ||
| 1801 | } | 1720 | } |
| 1802 | } | 1721 | } |
| 1803 | |||
| 1804 | if (url != NULL) { | ||
| 1805 | xs_html *content_attachments = xs_html_tag("div", | ||
| 1806 | xs_html_attr("class", "snac-content-attachments")); | ||
| 1807 | |||
| 1808 | xs_html_add(snac_content, | ||
| 1809 | content_attachments); | ||
| 1810 | |||
| 1811 | xs_html_add(content_attachments, | ||
| 1812 | xs_html_tag("video", | ||
| 1813 | xs_html_attr("preload", "none"), | ||
| 1814 | xs_html_attr("style", "width: 100%"), | ||
| 1815 | xs_html_attr("class", "snac-embedded-video"), | ||
| 1816 | xs_html_attr("controls", NULL), | ||
| 1817 | xs_html_attr("src", url), | ||
| 1818 | xs_html_text(L("Video")), | ||
| 1819 | xs_html_text(": "), | ||
| 1820 | xs_html_tag("a", | ||
| 1821 | xs_html_attr("href", url), | ||
| 1822 | xs_html_text("---")))); | ||
| 1823 | } | ||
| 1824 | } | 1722 | } |
| 1825 | 1723 | ||
| 1826 | /* has this message an audience (i.e., comes from a channel or community)? */ | 1724 | /* has this message an audience (i.e., comes from a channel or community)? */ |
| @@ -246,6 +246,7 @@ const char *default_avatar_base64(void); | |||
| 246 | xs_str *process_tags(snac *snac, const char *content, xs_list **tag); | 246 | xs_str *process_tags(snac *snac, const char *content, xs_list **tag); |
| 247 | 247 | ||
| 248 | char *get_atto(const xs_dict *msg); | 248 | char *get_atto(const xs_dict *msg); |
| 249 | xs_list *get_attachments(const xs_dict *msg); | ||
| 249 | 250 | ||
| 250 | xs_dict *msg_admiration(snac *snac, char *object, char *type); | 251 | xs_dict *msg_admiration(snac *snac, char *object, char *type); |
| 251 | xs_dict *msg_create(snac *snac, const xs_dict *object); | 252 | xs_dict *msg_create(snac *snac, const xs_dict *object); |