utils.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. {% macro display_actor_inline(follower, default_avatar, size=50,actions={}) -%}
  2. {% if follower and follower.id %}
  3. <div class="actor-box">
  4. <span style="float:left;padding-right:10px">
  5. {% if not follower.icon %}
  6. <img class="actor-icon" src="{{ default_avatar }}" style="width:50px;height:50px">
  7. {% else %}
  8. <img class="actor-icon" src="{{ follower.icon.url }}" style="width:50px;height:50px">{% endif %}
  9. </span>
  10. {% if actions.followbtn %}
  11. <div class="actor-button">
  12. <form action="/api/follow" class="action-form" method="POST">
  13. <input type="hidden" name="redirect" value="{{ request.path }}"/>
  14. <input type="hidden" name="actor" value="{{ follower.id }}"/>
  15. <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
  16. <button type="submit" class="bar-item pure-button">follow</button>
  17. </form>
  18. </div>
  19. {% endif %}
  20. {% if actions.unfollowbtn %}
  21. <div class="actor-button">
  22. <form action="/api/undo" class="action-form" method="POST">
  23. <input type="hidden" name="redirect" value="{{ request.path }}"/>
  24. <input type="hidden" name="id" value="{{ fid }}"/>
  25. <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
  26. <button type="submit" class="bar-item pure-button">unfollow</button>
  27. </form>
  28. </div>
  29. {% endif %}
  30. <div class="actor-inline">
  31. <a href="{{follower | url_or_id | get_url }}" style="font-weight:bold">{{ follower.name or follower.preferredUsername }}</a><br/>
  32. <small class="lcolor">@{{ follower.preferredUsername }}@{{ follower | url_or_id | get_url | domain }}</small>
  33. </div>
  34. </div>
  35. {% endif %}
  36. {%- endmacro %}
  37. {% macro display_note(obj, default_avatar, perma=False, ui=False, likes=[], shares=[], meta={}, no_color=False, index=False, limit=0) -%}
  38. {% if meta.actor %}
  39. {% set actor = meta.actor %}
  40. {% else %}
  41. {% set actor = obj.attributedTo | get_actor %}
  42. {% endif %}
  43. {% if session.logged_in %}
  44. {% set perma_id = obj.id | permalink_id %}
  45. {% if request.args.get('older_than') %}
  46. {% set redir = request.path + "?older_than=" + request.args.get('older_than') + "#activity-" + perma_id %}
  47. {% elif request.args.get('newer_than') %}
  48. {% set redir = request.path + "?newer_than=" + request.args.get('newer_than') + "#activity-" + perma_id %}
  49. {% else %}
  50. {% set redir = request.path + "#activity-" + perma_id %}
  51. {% endif %}
  52. {% set aid = obj.id | quote_plus %}
  53. {% endif %}
  54. <section class="post">
  55. <header class="post-header">
  56. <img width="48" height="48" class="post-avatar" src="{% if not actor.icon %}{{ default_avatar }}{% else %}{{ actor.icon.url }}{% endif %}">
  57. <p class="post-meta">
  58. <a href="{{ actor | url_or_id | get_url }}" class="no-hover"><strong>{{ actor.name or actor.preferredUsername }}</strong>
  59. <span class="l">@{% if not no_color and obj.id | is_from_outbox %}<span class="pcolor">{{ actor.preferredUsername }}</span>{% else %}{{ actor.preferredUsername }}{% endif %}@{% if not no_color and obj.id | is_from_outbox %}<span class="pcolor">{{ actor | url_or_id | get_url | domain }}</span>{% else %}{{ actor | url_or_id | get_url | domain }}{% endif %}</span></a> published
  60. <a rel="noopener" class="u-url u-uid note-permalink l" href="{{ obj | url_or_id | get_url }}"><time title="{{ obj.published }}" datetime="{{ obj.published }}">{{ obj.published | format_timeago }}</time></a>
  61. </p>
  62. </header>
  63. <div class="post-description">
  64. {% if obj.summary %}<p class="p-summary">{{ obj.summary | clean }}</p>{% endif %}
  65. {% if obj | has_type('Video') %}
  66. <div class="note-video">
  67. <video controls preload="metadata" src="{{ obj.url }}" width="480">
  68. </video>
  69. </div>
  70. {% endif %}
  71. <div class="note-container{% if perma %} perma{%endif%} p-name e-content">
  72. {% if obj | has_type('Article') %}
  73. {{ obj.name }} <a href="{{ obj | url_or_id | get_url }}">{{ obj | url_or_id | get_url }}</a>
  74. {% elif obj | has_type('Question') %}
  75. {% if index and limit != 0 and obj.content|length > limit %}
  76. {{ obj.content | truncate(limit) | clean | safe }}
  77. <a href="{{ obj | url_or_id | get_url }}">Read more.</a>
  78. {% else %}
  79. {{ obj.content | clean | safe }}
  80. {% endif %}
  81. {% if obj.id | is_from_outbox or (meta.question_replies and (obj.closed or meta.voted_for)) %}
  82. <ul style="list-style:none;padding:0;">
  83. {% set total_votes = meta.question_replies %}
  84. {% for oneOf in obj.oneOf %}
  85. {% set pct = 0 %}
  86. {% if total_votes > 0 %}
  87. {% set cnt = oneOf.name | get_answer_count(meta) %}
  88. {% set pct = cnt * 100.0 / total_votes %}
  89. {% endif %}
  90. <li class="answer">
  91. <span class="answer-bar color-menu-background" style="width:{{pct}}%;"></span>
  92. <span class="answer-text">
  93. <span>{{ '%0.0f'| format(pct) }}%</span>
  94. {{ oneOf.name }}
  95. </span>
  96. </li>
  97. {% endfor %}
  98. </ul>
  99. <p><small>
  100. {% if obj.closed or obj.endTime | gtnow %}
  101. Ended {{ obj.endTime | format_timeago }} with <strong>{{ total_votes }}</strong> vote{% if total_votes | gtone %}s{% endif %}.
  102. {% else %}
  103. Ends {{ obj.endTime | format_timeago }} (<strong>{{ total_votes }}</strong> vote{% if total_votes | gtone %}s{% endif %} as of now).
  104. {% endif %}
  105. </small></p>
  106. {% else %}
  107. <ul style="list-style:none;padding:0;">
  108. {% for oneOf in obj.oneOf %}
  109. <li class="answer">
  110. <span class="answer-text">
  111. {% if not meta.voted_for and not obj.endTime | gtnow %}
  112. <span><form action="/api/vote" class="action-form" method="POST">
  113. <input type="hidden" name="redirect" value="{{ redir }}">
  114. <input type="hidden" name="id" value="{{ obj.id }}">
  115. <input type="hidden" name="choice" value="{{ oneOf.name }}">
  116. <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
  117. <button type="submit" class="bar-item">vote</button>
  118. </form></span>
  119. {% else %}
  120. <span>???</span>
  121. {% endif %}
  122. {{ oneOf.name }} {% if oneOf.name == meta.voted_for %}(your vote){% endif %}
  123. </span>
  124. </li>
  125. {% endfor %}
  126. <p><small>{% if obj.endTime | gtnow %}This question ended {{ obj.endTime | format_timeago }}.</small></p>
  127. {% else %}This question ends {{ obj.endTime | format_timeago }}{% endif %}
  128. </small></p>
  129. </ul>
  130. {% endif %}
  131. {% else %}
  132. {% if index and limit != 0 and obj.content|length > limit %}
  133. {{ obj.content | truncate(limit) | clean | safe }}
  134. <a href="{{ obj | url_or_id | get_url }}">Read more.</a>
  135. {% else %}
  136. {{ obj.content | clean | safe }}
  137. {% endif %}
  138. {% endif %}
  139. </div>
  140. {% if obj.attachment and obj | has_type('Note') %}
  141. <div style="padding:20px 0;">
  142. {% if obj.attachment | not_only_imgs %}
  143. <h3 class="l">Attachments</h3>
  144. <ul style="padding:0;">
  145. {% endif %}
  146. {% for a in obj.attachment %}
  147. {% if (a.mediaType and a.mediaType.startswith("image/")) or (a.type and a.type == 'Image') %}
  148. <a href="{{ a.url | get_attachment_url(None) }}"><img src="{{a.url }}" class="img-attachment"></a>
  149. {% elif (a.mediaType and a.mediaType.startswith("video/")) %}
  150. <li><video controls preload="metadata" src="{{ a.url }}" width="480"></video></li>
  151. {% else %}
  152. <li><a href="{{a.url }}" class="l">{% if a.filename %}{{ a.filename }}{% else %}{{ a.url }}{% endif %}</a></li>
  153. {% endif %}
  154. {% endfor %}
  155. {% if obj.attachment | not_only_imgs %}
  156. </ul>
  157. {% endif %}
  158. </div>
  159. {% endif %}
  160. {% if meta and meta.og_metadata and obj | has_type('Note') %}
  161. {% for og in meta.og_metadata %}
  162. {% if og.url %}
  163. <a href="{{ og.url }}" class="og-link" style="margin:30px 0;clear:both;display: flex;">
  164. <div>
  165. <img style="width:100px;border-radius:3px;" src="{{ og.image }}">
  166. </div>
  167. <div style="padding:0 20px;">
  168. <strong>{{ og.title }}</strong>
  169. <p>{{ og.description | truncate(80) }}</p>
  170. <small>{{ og.site_name }}</small>
  171. </div>
  172. </a>
  173. {% endif %}
  174. {% endfor %}
  175. {% endif %}
  176. {% if perma %}<span class="perma-item" style="float:right">{{ obj.published | format_time }}</span>{% endif %}
  177. <div class="bottom-bar pure-button-group" role="group">
  178. {% if perma %}
  179. {% if not (obj.id | is_from_outbox) %}
  180. <a class="bar-item pure-button" href="{{ obj | url_or_id | get_url }}">permalink</a>
  181. {% endif %}
  182. {% else %}
  183. <a class="bar-item pure-button" href="{{ obj | url_or_id | get_url }}">permalink</a>
  184. {% endif %}
  185. {% if meta.count_reply and obj.id | is_from_outbox %}<a class ="bar-item" href="{{ obj.url | get_url }}"><strong>{{ meta.count_reply }}</strong> replies</a>
  186. {% elif meta.count_reply and session.logged_in %}
  187. <a class="bar-item pure-button" href="/admin/thread?oid={{aid}}"><strong>{{ meta.count_reply }}</strong> replies</a>{% endif %}
  188. {% if not perma and meta.count_boost and obj.id | is_from_outbox %}<a class="bar-item pure-button" href="{{ obj.url | get_url }}"><strong>{{ meta.count_boost }}</strong> boosts</a>{% endif %}
  189. {% if not perma and meta.count_like and obj.id | is_from_outbox %}<a class="bar-item pure-button" href="{{ obj.url | get_url }}"><strong>{{ meta.count_like }}</strong> likes</a>{% endif %}
  190. {% if session.logged_in %}
  191. {% if ui %}
  192. {% if meta.boosted %}
  193. <form action="/api/undo" class="action-form" method="POST">
  194. <input type="hidden" name="redirect" value="{{ redir }}">
  195. <input type="hidden" name="id" value="{{ meta.boosted }}">
  196. <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
  197. <button type="submit" class="bar-item pure-button">unboost</button>
  198. </form>
  199. {% else %}
  200. <form action="/api/boost" class="action-form" method="POST">
  201. <input type="hidden" name="redirect" value="{{ redir }}">
  202. <input type="hidden" name="id" value="{{ obj.id }}">
  203. <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
  204. <button type="submit" class="bar-item pure-button">boost</button>
  205. </form>
  206. {% endif %}
  207. {% if meta.liked %}
  208. <form action="/api/undo" class="action-form" method="POST">
  209. <input type="hidden" name="redirect" value="{{ redir }}">
  210. <input type="hidden" name="id" value="{{ meta.liked }}">
  211. <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
  212. <button type="submit" class="bar-item pure-button">unlike</button>
  213. </form>
  214. {% else %}
  215. <form action="/api/like" class="action-form" method="POST">
  216. <input type="hidden" name="redirect" value="{{ redir }}">
  217. <input type="hidden" name="id" value="{{ obj.id }}">
  218. <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
  219. <button type="submit" class="bar-item pure-button">like</button>
  220. </form>
  221. {% endif %}
  222. {% endif %}
  223. {% if obj.id | is_from_outbox %}
  224. <form action="/api/note/delete" class="action-form" method="POST">
  225. <input type="hidden" name="redirect" value="{{ redir }}">
  226. <input type="hidden" name="id" value="{{ obj.id }}">
  227. <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
  228. <button type="submit" class="bar-item pure-button">delete</button>
  229. </form>
  230. {% if meta.pinned %}
  231. <form action="/api/note/unpin" class="action-form" method="POST">
  232. <input type="hidden" name="redirect" value="{{ redir }}">
  233. <input type="hidden" name="id" value="{{ obj.id }}">
  234. <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
  235. <button type="submit" class="bar-item pure-button">unpin</button>
  236. </form>
  237. {% else %}
  238. <form action="/api/note/pin" class="action-form" method="POST">
  239. <input type="hidden" name="redirect" value="{{ redir }}">
  240. <input type="hidden" name="id" value="{{ obj.id }}">
  241. <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
  242. <button type="submit" class="bar-item pure-button">pin</button>
  243. </form>
  244. {% endif %}
  245. {% else %}
  246. <form action="/api/block" class="action-form" method="POST">
  247. <input type="hidden" name="redirect" value="{{ redir }}">
  248. <input type="hidden" name="actor" value="{{ actor.id }}">
  249. <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
  250. <button type="submit" class="bar-item pure-button">block</button>
  251. </form>
  252. {% endif %}
  253. <a class="bar-item pure-button" href="/admin/new?reply={{ aid }}">reply</a>
  254. {% endif %}
  255. </div>
  256. {% if likes or shares %}
  257. <div style="padding-top:20px;" class="pure-g">
  258. {% if likes %}
  259. <div class="pure-u-1-2">
  260. <h4 style="font-weight:normal"><strong>{{ likes|length }}</strong> likes</h4>{% for like in likes %}
  261. {{ display_actor_inline(like) }}
  262. {% endfor %}
  263. </div>
  264. {% endif %}
  265. {% if shares %}
  266. <div class="pure-u-1-2">
  267. <h4 style="font-weight:normal"><strong>{{ shares|length }}</strong> boosts</h4>{% for boost in shares %}
  268. {{ display_actor_inline(boost) }}
  269. {% endfor %}
  270. </div>
  271. {% endif %}
  272. </div>
  273. {% endif %}
  274. </div>
  275. </section>
  276. {%- endmacro %}
  277. {% macro display_thread(thread, default_avatar, likes=[], shares=[]) -%}
  278. {% for reply in thread %}
  279. {% if reply._requested %}
  280. {{ display_note(reply.activity.object, default_avatar=default_avatar, perma=True, ui=False, likes=likes, shares=shares, meta=reply.meta) }}
  281. {% else %}
  282. {{ display_note(reply.activity.object, default_avatar=default_avatar, perma=False, ui=True, meta=reply.meta) }}
  283. {% endif %}
  284. {% endfor %}
  285. {% endmacro -%}
  286. {% macro display_pagination(older_than, newer_than) -%}
  287. <div class="clear">
  288. {% if older_than %}
  289. <a href="{{ config.BASE_URL }}{{ request.path }}?older_than={{older_than}}{% if request.args.get('limit') %}&limit={{request.args.get('limit')}}{% endif %}" rel="next" class="older-link lcolor"><span class="pcolor">🡨</span> Older</a>
  290. {% endif %}
  291. {% if newer_than %}
  292. <a href="{{ config.BASE_URL }}{{ request.path }}?newer_than={{newer_than}}{% if request.args.get('limit') %}&limit={{request.args.get('limit')}}{% endif %}" rel="prev" class="newer-link lcolor">Newer <span class="pcolor">🡪</span></a>
  293. {% endif %}
  294. </div>
  295. {% endmacro -%}
  296. {% macro display_pagination_links(older_than, newer_than) -%}
  297. {% if older_than %}<link rel="next" href="{{ config.BASE_URL }}{{ request.path }}?older_than={{older_than}}">{% endif %}
  298. {% if newer_than %}<link rel="prev" href="{{ config.BASE_URL }}{{ request.path }}?newer_than={{newer_than}}">{% endif %}
  299. {% endmacro -%}