New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor link previews #1276
Refactor link previews #1276
Conversation
xPaw
commented
Jun 26, 2017
•
edited
edited
- Moves preview content as part of the original message
- Renders toggle button text with CSS so it's not selectable and not copyable
- Toggle button only appears after getting preview content, not instantly
- This will fix Link preview button shown on invalid links #1173
- Fixes auto opening previews in history and page reloads
- Fix image previews broken by one of the previous PRs
- Add more tests
617e1b2
to
cd157bd
Compare
client/views/msg.tpl
Outdated
{{#if preview}} | ||
{{> toggle}} | ||
{{/if}} | ||
</span> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
extra wrong span?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is that?
cd157bd
to
8a9e72c
Compare
Putting this here for the record: it would be really really really great if you could make this its own PR so we could merge that ASAP. I don't think I'll have time to review this this week or for v2.3.3. Thanks! |
@@ -1,19 +1,18 @@ | |||
{{#toggle}} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you mean to rename this file into msg_preview as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Renamed.
@@ -1110,6 +1106,10 @@ kbd { | |||
padding: 0 6px; | |||
} | |||
|
|||
#chat .toggle-button:after { | |||
content: "···"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably for another PR, but I'd love to change this to a right (closed) / down (opened) caret...
@@ -37,7 +37,7 @@ import jQuery from "jquery"; | |||
lastStick = Date.now(); | |||
this.scrollTop = this.scrollHeight; | |||
}) | |||
.on("msg.sticky", keepToBottom) | |||
.on("keepToBottom.sticky", keepToBottom) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm definitely missing something here, why does this need to be changed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gotta trigger "keep to bottom" logic, but can't call the already used msg
event.
client/js/render.js
Outdated
(options.links && data.msg.preview.type === "link") || | ||
(options.thumbnails && data.msg.preview.type === "image"); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any chance we can avoid the repetition between this and msg_preview.js?
@@ -35,7 +35,8 @@ socket.on("msg", function(data) { | |||
.trigger("msg", [ | |||
target, | |||
data | |||
]); | |||
]) | |||
.trigger("keepToBottom"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this mean that anytime a message is received, the channel is jumped to the bottom automatically?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Testing this, it doesn't seem so. But then I'm not 100% why this is for.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because keepToBottom
has logic inside to only "keep to bottom" if it's already at the bottom. Thats's the point of the plugin.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, this goes along with the rename from msg to keepToBottom and I missed that.
src/plugins/irc-events/link.js
Outdated
.replace(/\x02|\x1D|\x1F|\x16|\x0F|\x03(?:[0-9]{1,2}(?:,[0-9]{1,2})?)?/g, "") | ||
.split(" ") | ||
.filter((w) => /^https?:\/\//.test(w)); | ||
const cleanText = msg.text.replace(/\x02|\x1D|\x1F|\x16|\x0F|\x03(?:[0-9]{1,2}(?:,[0-9]{1,2})?)?/g, ""); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add a comment to explain what this regex does, please?
const request = require("request"); | ||
const Helper = require("../../helper"); | ||
const findLinks = require("../../../client/js/libs/handlebars/ircmessageparser/findLinks"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oooh I like where this is going!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, I just realized (:man_facepalming:) that this server file is loading a client file. Any way we can make this a bit cleaner? Not sure what's the best way, I'm just wondering.
src/plugins/irc-events/link.js
Outdated
.split(" ") | ||
.filter((w) => /^https?:\/\//.test(w)); | ||
const cleanText = msg.text.replace(/\x02|\x1D|\x1F|\x16|\x0F|\x03(?:[0-9]{1,2}(?:,[0-9]{1,2})?)?/g, ""); | ||
const links = findLinks(cleanText).filter((w) => /^https?:\/\//.test(w.link)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be slick to add a scheme parameters to the findLinks function. Only a single usage for this right now but I can see where this could be useful.
if (!toggle.head.length) { | ||
if (toggle.thumb.length || toggle.body.length) { | ||
toggle.head = "Untitled page"; | ||
if (!preview.head.length && preview.type === "link") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the record, this is the fix for that bug introduced by a previous PR: image previews were broken.
test/plugins/link.js
Outdated
|
||
describe("Link plugin", function() { | ||
before(function(done) { | ||
this.app = util.createWebserver(); | ||
this.app.get("/favicon.png", function(req, res) { | ||
res.sendFile(path.resolve(__dirname, "../../client/img/apple-touch-icon-120x120.png")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Our prefetcher fetches favicons as a thumbnail?! At https://github.com/thelounge/lounge/pull/1276/files#diff-4805d608cbc31851a7bee1bf4c7e247eR54 I'm only seeing og:image
and twitter:image
, what am I missing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is for image previews, not thumbnails. This is just a test URL that serves an existing image so the image preview passes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah right. I know this is nitpicky, but could you rename to test-image or something? It's kind of confusing at first glance, you think the prefetcher makes use of the favicon or something (which is not so far fetched, hence the confusion).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Eh, do we want to add an unnecessary image to the repo?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No no I meant rename favicon.png to test-image.png or something. I'm guessing the name is just a random placeholder, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yea, will do
toggle.type = "link"; | ||
toggle.head = | ||
preview.type = "link"; | ||
preview.head = | ||
$("meta[property=\"og:title\"]").attr("content") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it worth making use of og:site_name
too? Like site_name - title
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find that unnecessary noise.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair enough, we'll see at usage if that's something that would make sense or not
b62a855
to
9f84611
Compare
@astorije Addressed your comments. EDIT: I need to still call |
9f84611
to
718b87e
Compare
Okay fixed, should be good to go. |
718b87e
to
14b2ad7
Compare
For the record, when it comes to code coverage, what this PR does: Before:
After:
I was actually expecting more based on the tests in this PR, but I'll take it! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This works really well, 👏
Merging, with a single review, I know, but it should actually be straightforward, plus we have limited maintainers' availability right now.
We'll find out soon enough if there are hidden bugs or not.
#1276 refactored previews, and in particular [reused our link-in-messages detection](https://github.com/thelounge/lounge/pull/1276/files#diff-4805d608cbc31851a7bee1bf4c7e247eR6) instead of [having a separate logic](https://github.com/thelounge/lounge/pull/1276/files#diff-4805d608cbc31851a7bee1bf4c7e247eL19). Unfortunately, this loads a client library, which requires `urijs`. Since these are built at release time, we were not including this package in the production dependencies, and it is now breaking at install time. This might happen again if we add a client dependency in this file and forget it is also loaded by the server. We could in the future either extract this logic into a shared location, or we could move this logic entirely on the server (or maybe many other options), but in the meantime this will fix the issue in `v2.4.0-rc.1`.
thelounge#1276 refactored previews, and in particular [reused our link-in-messages detection](https://github.com/thelounge/lounge/pull/1276/files#diff-4805d608cbc31851a7bee1bf4c7e247eR6) instead of [having a separate logic](https://github.com/thelounge/lounge/pull/1276/files#diff-4805d608cbc31851a7bee1bf4c7e247eL19). Unfortunately, this loads a client library, which requires `urijs`. Since these are built at release time, we were not including this package in the production dependencies, and it is now breaking at install time. This might happen again if we add a client dependency in this file and forget it is also loaded by the server. We could in the future either extract this logic into a shared location, or we could move this logic entirely on the server (or maybe many other options), but in the meantime this will fix the issue in `v2.4.0-rc.1`.
Refactor link previews
thelounge#1276 refactored previews, and in particular [reused our link-in-messages detection](https://github.com/thelounge/lounge/pull/1276/files#diff-4805d608cbc31851a7bee1bf4c7e247eR6) instead of [having a separate logic](https://github.com/thelounge/lounge/pull/1276/files#diff-4805d608cbc31851a7bee1bf4c7e247eL19). Unfortunately, this loads a client library, which requires `urijs`. Since these are built at release time, we were not including this package in the production dependencies, and it is now breaking at install time. This might happen again if we add a client dependency in this file and forget it is also loaded by the server. We could in the future either extract this logic into a shared location, or we could move this logic entirely on the server (or maybe many other options), but in the meantime this will fix the issue in `v2.4.0-rc.1`.