Skip to content
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

Merged
merged 1 commit into from Jul 3, 2017
Merged

Refactor link previews #1276

merged 1 commit into from Jul 3, 2017

Conversation

xPaw
Copy link
Member

@xPaw xPaw commented Jun 26, 2017

  • 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
  • Fixes auto opening previews in history and page reloads
  • Fix image previews broken by one of the previous PRs
  • Add more tests

@xPaw xPaw added Type: Bug Issues that report and PRs that solve any defects that cause unexpected behaviors. Type: Feature Tickets that describe a desired feature or PRs that add them to the project. labels Jun 26, 2017
@xPaw xPaw added this to the 2.3.3 milestone Jun 26, 2017
{{#if preview}}
{{> toggle}}
{{/if}}
</span>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extra wrong span?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is that?

@astorije
Copy link
Member

Fix image previews broken by one of the previous PRs

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}}
Copy link
Member

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?

Copy link
Member Author

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: "···";
Copy link
Member

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)
Copy link
Member

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?

Copy link
Member Author

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.

(options.links && data.msg.preview.type === "link") ||
(options.thumbnails && data.msg.preview.type === "image");
}

Copy link
Member

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");
Copy link
Member

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?

Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Member

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.

.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, "");
Copy link
Member

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");
Copy link
Member

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!

Copy link
Member

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.

.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));
Copy link
Member

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") {
Copy link
Member

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.


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"));
Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Member

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).

Copy link
Member Author

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?

Copy link
Member

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?

Copy link
Member Author

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")
Copy link
Member

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

Copy link
Member Author

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.

Copy link
Member

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

@xPaw
Copy link
Member Author

xPaw commented Jul 2, 2017

@astorije Addressed your comments.

EDIT: I need to still call click event handler, as that has a fix for image load and scroll to bottom.

@xPaw
Copy link
Member Author

xPaw commented Jul 2, 2017

Okay fixed, should be good to go.

@astorije
Copy link
Member

astorije commented Jul 3, 2017

For the record, when it comes to code coverage, what this PR does:

Before:

Statements   : 20.65% ( 741/3589 )
Branches     : 11.67% ( 196/1680 )
Functions    : 22.42% ( 115/513 )
Lines        : 23.95% ( 735/3069 )

After:

Statements   : 20.93% ( 752/3593 )
Branches     : 12.75% ( 215/1686 )
Functions    : 22.52% ( 116/515 )
Lines        : 24.25% ( 745/3072 )

I was actually expecting more based on the tests in this PR, but I'll take it!

Copy link
Member

@astorije astorije left a 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.

@astorije astorije merged commit 622c91b into master Jul 3, 2017
@astorije astorije deleted the xpaw/refactor-toggle branch July 3, 2017 06:33
astorije added a commit that referenced this pull request Jul 27, 2017
#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`.
eliemichel pushed a commit to eliemichel/lounge that referenced this pull request Aug 31, 2017
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`.
matburnham pushed a commit to matburnham/lounge that referenced this pull request Sep 6, 2017
matburnham pushed a commit to matburnham/lounge that referenced this pull request Sep 6, 2017
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`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Bug Issues that report and PRs that solve any defects that cause unexpected behaviors. Type: Feature Tickets that describe a desired feature or PRs that add them to the project.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Link preview button shown on invalid links
2 participants