mirror of
https://github.com/TeamPiped/Piped-Backend.git
synced 2024-08-14 23:51:41 +00:00
Implement channel data federation.
This commit is contained in:
parent
92cee87822
commit
005bbfd1e5
4 changed files with 113 additions and 26 deletions
|
@ -7,6 +7,7 @@ import me.kavin.piped.ipfs.IPFS;
|
||||||
import me.kavin.piped.utils.*;
|
import me.kavin.piped.utils.*;
|
||||||
import me.kavin.piped.utils.obj.*;
|
import me.kavin.piped.utils.obj.*;
|
||||||
import me.kavin.piped.utils.obj.db.Video;
|
import me.kavin.piped.utils.obj.db.Video;
|
||||||
|
import me.kavin.piped.utils.obj.federation.FederatedChannelInfo;
|
||||||
import me.kavin.piped.utils.obj.federation.FederatedVideoInfo;
|
import me.kavin.piped.utils.obj.federation.FederatedVideoInfo;
|
||||||
import me.kavin.piped.utils.resp.InvalidRequestResponse;
|
import me.kavin.piped.utils.resp.InvalidRequestResponse;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
@ -57,6 +58,14 @@ public class ChannelHandlers {
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
Multithreading.runAsync(() -> {
|
||||||
|
try {
|
||||||
|
MatrixHelper.sendEvent("video.piped.channel.info", new FederatedChannelInfo(info.getId(), info.getName(), info.getAvatarUrl(), info.isVerified()));
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
Multithreading.runAsync(() -> {
|
Multithreading.runAsync(() -> {
|
||||||
|
|
||||||
var channel = DatabaseHelper.getChannelFromId(info.getId());
|
var channel = DatabaseHelper.getChannelFromId(info.getId());
|
||||||
|
@ -65,28 +74,7 @@ public class ChannelHandlers {
|
||||||
|
|
||||||
if (channel != null) {
|
if (channel != null) {
|
||||||
|
|
||||||
boolean modified = false;
|
ChannelHelpers.updateChannel(s, channel, info.getName(), info.getAvatarUrl(), info.isVerified());
|
||||||
|
|
||||||
if (channel.isVerified() != info.isVerified()) {
|
|
||||||
channel.setVerified(info.isVerified());
|
|
||||||
modified = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!channel.getUploaderAvatar().equals(info.getAvatarUrl())) {
|
|
||||||
channel.setUploaderAvatar(info.getAvatarUrl());
|
|
||||||
modified = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!channel.getUploader().equals(info.getName())) {
|
|
||||||
channel.setUploader(info.getName());
|
|
||||||
modified = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (modified) {
|
|
||||||
var tr = s.beginTransaction();
|
|
||||||
s.update(channel);
|
|
||||||
tr.commit();
|
|
||||||
}
|
|
||||||
|
|
||||||
Set<String> ids = info.getRelatedItems()
|
Set<String> ids = info.getRelatedItems()
|
||||||
.stream()
|
.stream()
|
||||||
|
|
57
src/main/java/me/kavin/piped/utils/ChannelHelpers.java
Normal file
57
src/main/java/me/kavin/piped/utils/ChannelHelpers.java
Normal file
|
@ -0,0 +1,57 @@
|
||||||
|
package me.kavin.piped.utils;
|
||||||
|
|
||||||
|
import me.kavin.piped.consts.Constants;
|
||||||
|
import me.kavin.piped.utils.obj.db.Channel;
|
||||||
|
import okhttp3.Request;
|
||||||
|
import org.hibernate.StatelessSession;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.MalformedURLException;
|
||||||
|
import java.net.URL;
|
||||||
|
|
||||||
|
public class ChannelHelpers {
|
||||||
|
|
||||||
|
public static void updateChannel(StatelessSession s, Channel channel, String name, String avatarUrl, boolean uploaderVerified) {
|
||||||
|
|
||||||
|
boolean changed = false;
|
||||||
|
|
||||||
|
if (name != null && !name.equals(channel.getUploader())) {
|
||||||
|
channel.setUploader(name);
|
||||||
|
changed = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (avatarUrl != null && !avatarUrl.equals(channel.getUploaderAvatar())) {
|
||||||
|
|
||||||
|
URL url;
|
||||||
|
try {
|
||||||
|
url = new URL(avatarUrl);
|
||||||
|
if (!url.getHost().endsWith(".ggpht.com"))
|
||||||
|
return;
|
||||||
|
} catch (MalformedURLException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
try (var resp = Constants.h2client.newCall(new Request.Builder().url(url).head().build()).execute()) {
|
||||||
|
|
||||||
|
if (resp.isSuccessful())
|
||||||
|
channel.setUploaderAvatar(avatarUrl);
|
||||||
|
|
||||||
|
changed = true;
|
||||||
|
} catch (IOException e) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (uploaderVerified != channel.isVerified()) {
|
||||||
|
channel.setVerified(uploaderVerified);
|
||||||
|
changed = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (changed) {
|
||||||
|
var tr = s.beginTransaction();
|
||||||
|
s.update(channel);
|
||||||
|
tr.commit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -134,9 +134,11 @@ public class SyncRunner implements Runnable {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var content = event.at("/content/content");
|
||||||
|
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case "video.piped.stream.info" -> {
|
case "video.piped.stream.info" -> {
|
||||||
FederatedVideoInfo info = mapper.treeToValue(event.at("/content/content"), FederatedVideoInfo.class);
|
FederatedVideoInfo info = mapper.treeToValue(content, FederatedVideoInfo.class);
|
||||||
Multithreading.runAsync(() -> {
|
Multithreading.runAsync(() -> {
|
||||||
try (StatelessSession s = DatabaseSessionFactory.createStatelessSession()) {
|
try (StatelessSession s = DatabaseSessionFactory.createStatelessSession()) {
|
||||||
var video = DatabaseHelper.getVideoFromId(s, info.getVideoId());
|
var video = DatabaseHelper.getVideoFromId(s, info.getVideoId());
|
||||||
|
@ -153,8 +155,17 @@ public class SyncRunner implements Runnable {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
case "video.piped.channel.info" -> {
|
case "video.piped.channel.info" -> {
|
||||||
FederatedChannelInfo info = mapper.treeToValue(event.at("/content/content"), FederatedChannelInfo.class);
|
FederatedChannelInfo info = mapper.treeToValue(content, FederatedChannelInfo.class);
|
||||||
// TODO: Handle and send channel updates
|
Multithreading.runAsync(() -> {
|
||||||
|
try (StatelessSession s = DatabaseSessionFactory.createStatelessSession()) {
|
||||||
|
var channel = DatabaseHelper.getChannelFromId(s, info.getId());
|
||||||
|
if (channel != null)
|
||||||
|
ChannelHelpers.updateChannel(s, channel,
|
||||||
|
info.getName(),
|
||||||
|
info.getUploaderUrl(),
|
||||||
|
info.isVerified());
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
default -> System.err.println("Unknown event type: " + type);
|
default -> System.err.println("Unknown event type: " + type);
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,37 @@ package me.kavin.piped.utils.obj.federation;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||||
|
|
||||||
@JsonIgnoreProperties
|
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||||
public class FederatedChannelInfo {
|
public class FederatedChannelInfo {
|
||||||
|
|
||||||
|
private String id;
|
||||||
|
private String name;
|
||||||
|
private String uploaderUrl;
|
||||||
|
private boolean verified;
|
||||||
|
|
||||||
|
public FederatedChannelInfo() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public FederatedChannelInfo(String id, String name, String uploaderUrl, boolean verified) {
|
||||||
|
this.id = id;
|
||||||
|
this.name = name;
|
||||||
|
this.uploaderUrl = uploaderUrl;
|
||||||
|
this.verified = verified;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUploaderUrl() {
|
||||||
|
return uploaderUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isVerified() {
|
||||||
|
return verified;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue