Welcome to mirror list, hosted at ThFree Co, Russian Federation.

webmentions.js « js « assets - github.com/leonardofaria/bento.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c925a38f0102488164aab3c52075e79ba98295e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// based on https://shindakun.dev/posts/adding-webmentions-to-microblog/

// user-circle icon from heroicons.com encoded by https://yoksel.github.io/url-encoder/
const ANON_AVATAR = 'data:image/svg+xml,%3Csvg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor"%3E%3Cpath fill-rule="evenodd" d="M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-6-3a2 2 0 11-4 0 2 2 0 014 0zm-2 4a5 5 0 00-4.546 2.916A5.986 5.986 0 0010 16a5.986 5.986 0 004.546-2.084A5 5 0 0010 11z" clip-rule="evenodd" /%3E%3C/svg%3E';

const fetchWebmentions = (url, aliases, productionBaseUrl) => {
  if (!url) {
    url = document.location.origin + document.location.pathname;
  }
  const targets = getUrlPermutations(url, aliases, productionBaseUrl);

  const script = document.createElement('script');
  let src =
    'https://webmention.io/api/mentions?perPage=500&jsonp=parseWebmentions';
  targets.forEach((targetUrl) => {
    src += `&target[]=${encodeURIComponent(targetUrl)}`;
  });
  src += `&_=${Math.random()}`;
  script.src = src;
  script.async = true;
  document.body.appendChild(script);
}

const getUrlPermutations = (url, aliases, productionBaseUrl) => {
  const urls = [];

  // Replace the current base URL (ex.: http://localhost:1313) to the production one
  const currentUrl = new URL(window.location);
  url = url.replace(currentUrl.origin, productionBaseUrl);
  urls.push(url);

  // Add http variation
  urls.push(url.replace('https://', 'http://'));

  // Add variation with final /
  if (url.substr(-1) === '/') {
    var noslash = url.substr(0, url.length - 1);
    urls.push(noslash);
    urls.push(noslash.replace('https://', 'http://'));
  }

  // Add aliases variations
  if (aliases) {
    aliases.forEach((alias) => {
      urls.push(`${productionBaseUrl}/${alias}`);
    });
  }

  return urls;
}

const parseWebmentions = (data) => {
  const links = data.links.sort(wmSort);
  const likes = [];
  const reposts = [];
  const replies = [];
  const avatarUrls = [];

  links.map((l) => {
    if (!l.activity || !l.activity.type) {
      console.warning('unknown link type', l);
      return;
    }
    if (!l.verified) {
      return;
    }
    if (l.data.author) {
      avatarUrls.push(l.data.author.photo);
    }
    switch (l.activity.type) {
      case 'like':
        likes.push(l);
        break;
      case 'link':
        reposts.push(l);
        break;
      default:
        replies.push(l);
        break;
    }
  });

  resetContainers();
  render(likes, 'like-template', 'likes');
  render(reposts, 'reply-template', 'shares');
  render(replies, 'reply-template', 'replies');
  renderSummary(likes.length, (reposts.length + replies.length), avatarUrls);
}
window.parseWebmentions = parseWebmentions;

// Reset containers due to the usage of Turbolinks
const resetContainers = () => {
  const likes = document.querySelector('#likes');
  const shares = document.querySelector('#shares');
  const replies = document.querySelector('#replies');
  const avatars = document.querySelector('#webmentions-avatars');

  while (likes.lastChild) { likes.removeChild(likes.lastChild); }
  while (shares.lastChild) { shares.removeChild(shares.lastChild); }
  while (replies.lastChild) { replies.removeChild(replies.lastChild); }
  while (avatars.lastChild) { avatars.removeChild(avatars.lastChild); }
}

const wmSort = (a, b) => {
  const dateA = getWmDate(a);
  const dateB = getWmDate(b);
  if (dateA < dateB) {
    return -1;
  } else if (dateB < dateA) {
    return 1;
  }
  return 0;
}

const getWmDate = (webmention) => {
  if (webmention.data.published) {
    return new Date(webmention.data.published);
  }
  return new Date(webmention.verified_date);
}

const render = (items, template, destination) => {
  const t = document.getElementById(template).content;
  const list = document.getElementById(destination);

  items.map((l) => {
    const data = {
      url: l.data.url,
      date: new Date(l.data.published || l.verified_date),
      content: l.data.content,
    };

    if (l.data.author) {
      data.photo = l.data.author.photo || ANON_AVATAR;
      data.name = l.data.author.name;
      data.authorUrl = l.data.author.url;  
    } else {
      data.photo = ANON_AVATAR;
      data.name = getHostName(l.data.url) || 'inbound link';
      data.authorUrl = l.data.url;
    }

    if (l.activity.sentence) {
      data.sentence = l.activity.sentence;
    }

    fillTemplate(t, data);
    const clone = document.importNode(t, true);
    list.appendChild(clone);
  });

}

const getHostName = (url) => {
  var a = document.createElement('a');
  a.href = url;
  return (a.hostname || '').replace('www.', '');
}

const fillTemplate = (template, vals) => {
  template.querySelector('.js-avatar').src = vals.photo;
  template.querySelector('.js-author').href = vals.authorUrl;
  template.querySelector('.js-author').title = vals.name;
  template.querySelector('.js-author-name').innerHTML = vals.name;
  template.querySelector('.js-author-name').href = vals.authorUrl;
  template.querySelector('.js-source').href = vals.url;
  if (vals.sentence && template.querySelector('.js-sentence')) {
    template.querySelector('.js-sentence').innerText = vals.sentence;
  }
  const date = template.querySelector('.js-date');
  if (date) {
    date.innerHTML = formatDate(vals.date);
  }
  if (vals.content) {
    template.querySelector('.js-content').innerHTML = vals.content;
  }
}

const formatDate = (date) => {
  if (!date) {
    return '';
  }
  return date.toLocaleDateString('en-US', {
    month: 'short',
    day: '2-digit',
    year: 'numeric'
  });
}

const renderSummary = (totalLikes, totalInteractions, avatars) => {
  document.querySelector('#webmentions-total-likes').innerText = totalLikes;
  document.querySelector('#webmentions-total-interactions').innerText = totalInteractions;
  const avatarsContainer = document.querySelector('#webmentions-avatars');
  const uniqueAvatars = [...new Set(avatars)];

  for (const [index, avatar] of uniqueAvatars.entries()) {
    const image = document.createElement("img");
    image.src = avatar;
    image.classList = 'inline-block h-8 w-8 rounded-full ring-2 ring-white';
    image.alt = '';

    avatarsContainer.appendChild(image);

    if (index > 9) {
      const more = document.createElement("span");
      more.classList = 'flex items-center justify-center text-xs bg-black text-white font-bold h-8 w-8 rounded-full ring-2 ring-white';
      more.innerHTML = `+${uniqueAvatars.length - 9}`;
      avatarsContainer.appendChild(more);

      break;
    }
  };
}