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

cherry pick of #612 #616

Merged
merged 1 commit into from
Sep 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Mlem.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
5016A2B32A67EC0700B257E8 /* NotificationDisplayer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5016A2B22A67EC0700B257E8 /* NotificationDisplayer.swift */; };
503A5D752A78EF3C00488C38 /* Encodable+Export.swift in Sources */ = {isa = PBXBuildFile; fileRef = 503A5D742A78EF3C00488C38 /* Encodable+Export.swift */; };
503BA26F2A2C94540052516C /* URL+Identifiable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 503BA26E2A2C94540052516C /* URL+Identifiable.swift */; };
504ECBAE2AB45B2A006C0B96 /* LemmyURL.swift in Sources */ = {isa = PBXBuildFile; fileRef = 504ECBAD2AB45B2A006C0B96 /* LemmyURL.swift */; };
504ECBB12AB4B101006C0B96 /* LemmyURLTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 504ECBB02AB4B101006C0B96 /* LemmyURLTests.swift */; };
5064D03D2A6DE0AA00B22EE3 /* Notifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5064D03C2A6DE0AA00B22EE3 /* Notifier.swift */; };
5064D03F2A6DE0DB00B22EE3 /* Notifier+Dependency.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5064D03E2A6DE0DB00B22EE3 /* Notifier+Dependency.swift */; };
5064D0412A6E63E000B22EE3 /* Task+Notifiable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5064D0402A6E63E000B22EE3 /* Task+Notifiable.swift */; };
Expand Down Expand Up @@ -405,6 +407,8 @@
5016A2B22A67EC0700B257E8 /* NotificationDisplayer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationDisplayer.swift; sourceTree = "<group>"; };
503A5D742A78EF3C00488C38 /* Encodable+Export.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Encodable+Export.swift"; sourceTree = "<group>"; };
503BA26E2A2C94540052516C /* URL+Identifiable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "URL+Identifiable.swift"; sourceTree = "<group>"; };
504ECBAD2AB45B2A006C0B96 /* LemmyURL.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LemmyURL.swift; sourceTree = "<group>"; };
504ECBB02AB4B101006C0B96 /* LemmyURLTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LemmyURLTests.swift; sourceTree = "<group>"; };
5064D03C2A6DE0AA00B22EE3 /* Notifier.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Notifier.swift; sourceTree = "<group>"; };
5064D03E2A6DE0DB00B22EE3 /* Notifier+Dependency.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Notifier+Dependency.swift"; sourceTree = "<group>"; };
5064D0402A6E63E000B22EE3 /* Task+Notifiable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Task+Notifiable.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -893,6 +897,14 @@
path = TabBar;
sourceTree = "<group>";
};
504ECBAF2AB4B0DF006C0B96 /* Model */ = {
isa = PBXGroup;
children = (
504ECBB02AB4B101006C0B96 /* LemmyURLTests.swift */,
);
path = Model;
sourceTree = "<group>";
};
5064D03B2A6DE05000B22EE3 /* Notifications */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -1249,6 +1261,7 @@
6363D5D927EE196A00E34822 /* MlemTests */ = {
isa = PBXGroup;
children = (
504ECBAF2AB4B0DF006C0B96 /* Model */,
50DBB8DE2A805770002870B1 /* Mocks */,
50C86AB82A7E507200277519 /* Persistence */,
6363D5DA27EE196A00E34822 /* MlemTests.swift */,
Expand Down Expand Up @@ -1310,6 +1323,7 @@
isa = PBXGroup;
children = (
637218032A3A2AAD008C4816 /* HierarchicalComment.swift */,
504ECBAD2AB45B2A006C0B96 /* LemmyURL.swift */,
);
path = Internal;
sourceTree = "<group>";
Expand Down Expand Up @@ -2254,6 +2268,7 @@
6D405B032A43E7DB00C65F9C /* Sidebar Header Label.swift in Sources */,
50C99B5E2A61F611005D57DD /* CommentRepository+Dependency.swift in Sources */,
632578182A29F83C00446A66 /* PostSortMenu.swift in Sources */,
504ECBAE2AB45B2A006C0B96 /* LemmyURL.swift in Sources */,
CDA217EA2A63093E00BDA173 /* ReportComment.swift in Sources */,
CDA217E82A63029B00BDA173 /* ReportMention.swift in Sources */,
508845CF2A3641160088E483 /* JSONDecoder+Default.swift in Sources */,
Expand Down Expand Up @@ -2487,6 +2502,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
504ECBB12AB4B101006C0B96 /* LemmyURLTests.swift in Sources */,
50DBB8E02A805836002870B1 /* MockErrorHandler.swift in Sources */,
6363D5DB27EE196A00E34822 /* MlemTests.swift in Sources */,
50DBB8E22A80F9E4002870B1 /* APICommunity+Mock.swift in Sources */,
Expand Down
27 changes: 27 additions & 0 deletions Mlem/API/Internal/LemmyURL.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// LemmyURL.swift
// Mlem
//
// Created by mormaer on 15/09/2023.
//
//

import Foundation

struct LemmyURL {
let url: URL

init?(string: String?) {
guard let string else {
return nil
}

if let url = URL(string: string) {
self.url = url
} else if let encoded = string.addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed), let url = URL(string: encoded) {
self.url = url
} else {
return nil
}
}
}
9 changes: 7 additions & 2 deletions Mlem/API/Models/Community/APICommunity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ struct APICommunity: Codable, Identifiable {
let nsfw: Bool
let actorId: URL
let local: Bool
let icon: URL?
let banner: URL?
let icon: String?
let banner: String?
let hidden: Bool
let postingRestrictedToMods: Bool
let instanceId: Int
Expand All @@ -44,3 +44,8 @@ extension APICommunity: Comparable {
return lhsFullCommunity < rhsFullCommunity
}
}

extension APICommunity {
var iconUrl: URL? { LemmyURL(string: icon)?.url }
var bannerUrl: URL? { LemmyURL(string: banner)?.url }
}
12 changes: 9 additions & 3 deletions Mlem/API/Models/Person/APIPerson.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ struct APIPerson: Decodable, Identifiable, Hashable {
let id: Int
let name: String
let displayName: String?
let avatar: URL?
let avatar: String?
let banned: Bool
let published: Date
let updated: Date?
let actorId: URL
let bio: String?
let local: Bool
let banner: URL?
let banner: String?
let deleted: Bool
let sharedInboxUrl: URL?
let sharedInboxUrl: String?
let matrixUserId: String?
let admin: Bool? // this is no longer returned on beehaw...
let botAccount: Bool
Expand All @@ -34,3 +34,9 @@ extension APIPerson: Equatable {
lhs.actorId == rhs.actorId
}
}

extension APIPerson {
var avatarUrl: URL? { LemmyURL(string: avatar)?.url }
var bannerUrl: URL? { LemmyURL(string: banner)?.url }
var sharedInboxLink: URL? { LemmyURL(string: sharedInboxUrl)?.url }
}
9 changes: 7 additions & 2 deletions Mlem/API/Models/Posts/APIPost.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Foundation
struct APIPost: Decodable {
let id: Int
let name: String
let url: URL?
let url: String?
let body: String?
let creatorId: Int
let communityId: Int
Expand All @@ -28,10 +28,15 @@ struct APIPost: Decodable {
let nsfw: Bool
let published: Date
let removed: Bool
let thumbnailUrl: URL?
let thumbnailUrl: String?
let updated: Date?
}

extension APIPost {
var linkUrl: URL? { LemmyURL(string: url)?.url }
var thumbnailImageUrl: URL? { LemmyURL(string: thumbnailUrl)?.url }
}

extension APIPost: Equatable {
static func == (lhs: APIPost, rhs: APIPost) -> Bool {
lhs.id == rhs.id
Expand Down
6 changes: 5 additions & 1 deletion Mlem/API/Models/Posts/APIPostReport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@ struct APIPostReport: Decodable {
let creatorId: Int
let postId: Int
let originalPostName: String
let originalPostUrl: URL?
let originalPostUrl: String?
let originalPostBody: String?
let reason: String
let resolved: Bool
let resolverId: Int?
let published: Date
let updated: Date?
}

extension APIPostReport {
var originalUrl: URL? { LemmyURL(string: originalPostUrl)?.url }
}
9 changes: 7 additions & 2 deletions Mlem/API/Models/Site/APISite.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ struct APISite: Decodable {
let name: String
let sidebar: String?
let published: Date
let icon: URL?
let banner: URL?
let icon: String?
let banner: String?
let description: String?
let actorId: String?
let lastRefreshedAt: Date
let inboxUrl: String
let publicKey: String
let instanceId: Int
}

extension APISite {
var iconUrl: URL? { LemmyURL(string: icon)?.url }
var bannerUrl: URL? { LemmyURL(string: banner)?.url }
}
4 changes: 2 additions & 2 deletions Mlem/Extensions/API Post View - Post Type.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import Foundation
extension APIPostView {
var postType: PostType {
// post with URL: either image or link
if let postUrl = post.url {
if let postUrl = post.linkUrl {
// if image, return image link, otherwise return thumbnail
return postUrl.isImage ? .image(postUrl) : .link(post.thumbnailUrl)
return postUrl.isImage ? .image(postUrl) : .link(post.thumbnailImageUrl)
}

// otherwise text, but post.body needs to be present, even if it's an empty string
Expand Down
8 changes: 4 additions & 4 deletions Mlem/Models/Trackers/Post Tracker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,11 @@ class PostTracker: FeedTracker<APIPostView> {
for postView in newPosts {
// preload user and community avatars--fetching both because we don't know which we'll need, but these are super tiny
// so it's probably not an API crime, right?
if let communityAvatarLink = postView.community.icon {
if let communityAvatarLink = postView.community.iconUrl {
imageRequests.append(ImageRequest(url: communityAvatarLink.withIcon64Parameters))
}

if let userAvatarLink = postView.creator.avatar {
if let userAvatarLink = postView.creator.avatarUrl {
imageRequests.append(ImageRequest(url: userAvatarLink.withIcon64Parameters))
}

Expand All @@ -114,7 +114,7 @@ class PostTracker: FeedTracker<APIPostView> {
imageRequests.append(ImageRequest(url: url, priority: .high))
case .link(let url):
// websites: load image and favicon
if let baseURL = postView.post.url?.host,
if let baseURL = postView.post.linkUrl?.host,
let favIconURL = URL(string: "https://www.google.com/s2/favicons?sz=64&domain=\(baseURL)") {
imageRequests.append(ImageRequest(url: favIconURL))
}
Expand Down
2 changes: 1 addition & 1 deletion Mlem/Views/Shared/Components/Thumbnail Image View.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ struct ThumbnailImageView: View {
case .link(let url):
CachedImage(url: url, shouldExpand: false, fixedSize: size)
.onTapGesture {
if let url = postView.post.url {
if let url = postView.post.linkUrl {
openURL(url)
markPostAsRead()
}
Expand Down
2 changes: 1 addition & 1 deletion Mlem/Views/Shared/Links/Community Link View.swift
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ struct CommunityLabel: View {
@ViewBuilder
private var communityAvatar: some View {
Group {
if let url = community.icon {
if let url = community.iconUrl {
CachedImage(url: url.withIcon64Parameters,
shouldExpand: false,
fixedSize: avatarSize,
Expand Down
2 changes: 1 addition & 1 deletion Mlem/Views/Shared/Links/User Profile Label.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ struct UserProfileLabel: View {
@ViewBuilder
private var userAvatar: some View {
Group {
if let userAvatarLink = user.avatar {
if let userAvatarLink = user.avatarUrl {
CachedImage(url: userAvatarLink,
shouldExpand: false,
fixedSize: avatarSize,
Expand Down
8 changes: 4 additions & 4 deletions Mlem/Views/Shared/Website Icon Complex.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ struct WebsiteIconComplex: View {

var faviconURL: URL? {
guard
let baseURL = post.url?.host,
let baseURL = post.linkUrl?.host,
let imageURL = URL(string: "https://www.google.com/s2/favicons?sz=64&domain=\(baseURL)")
else {
return nil
Expand All @@ -48,15 +48,15 @@ struct WebsiteIconComplex: View {
}

var linkHost: String {
if let url = post.url {
if let url = post.linkUrl {
return url.host ?? "some website"
}
return "some website"
}

var body: some View {
VStack(spacing: 0) {
if shouldShowWebsitePreviews, let thumbnailURL = post.thumbnailUrl {
if shouldShowWebsitePreviews, let thumbnailURL = post.thumbnailImageUrl {
CachedImage(url: thumbnailURL, shouldExpand: false)
.frame(maxHeight: 400)
.applyNsfwOverlay(post.nsfw)
Expand Down Expand Up @@ -97,7 +97,7 @@ struct WebsiteIconComplex: View {
)
.contentShape(Rectangle())
.onTapGesture {
if let url = post.url {
if let url = post.linkUrl {
openURL(url)
if let onTapActions {
onTapActions()
Expand Down
11 changes: 6 additions & 5 deletions Mlem/Views/Tabs/Feeds/Components/Sidebar View.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,10 @@ struct CommunitySidebarView: View {
title: communityDetails.communityView.community.name,
subtitle: "@\(communityDetails.communityView.community.name)@\(communityDetails.communityView.community.actorId.host()!)",
avatarSubtext: .constant("Created \(getRelativeTime(date: communityDetails.communityView.community.published))"),
bannerURL: shouldShowCommunityHeaders ? communityDetails.communityView.community.banner : nil,
avatarUrl: communityDetails.communityView.community.icon,
label1: "\(communityDetails.communityView.counts.subscribers) Subscribers")
bannerURL: shouldShowCommunityHeaders ? communityDetails.communityView.community.bannerUrl : nil,
avatarUrl: communityDetails.communityView.community.iconUrl,
label1: "\(communityDetails.communityView.counts.subscribers) Subscribers"
)

Picker(selection: $selectionSection, label: Text("Profile Section")) {
Text("Description").tag(0)
Expand Down Expand Up @@ -149,8 +150,8 @@ struct SidebarPreview: PreviewProvider {
nsfw: false,
actorId: URL(string: "https://lemmy.foo.com/c/testcommunity")!,
local: false,
icon: URL(string: "https://vlemmy.net/pictrs/image/190f2d6a-ac38-448d-ae9b-f6d751eb6e69.png?format=webp"),
banner: URL(string: "https://vlemmy.net/pictrs/image/719b61b3-8d8e-4aec-9f15-17be4a081f97.jpeg?format=webp"),
icon: "https://vlemmy.net/pictrs/image/190f2d6a-ac38-448d-ae9b-f6d751eb6e69.png?format=webp",
banner: "https://vlemmy.net/pictrs/image/719b61b3-8d8e-4aec-9f15-17be4a081f97.jpeg?format=webp",
hidden: false,
postingRestrictedToMods: false,
instanceId: 0
Expand Down
10 changes: 5 additions & 5 deletions Mlem/Views/Tabs/Profile/User View.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ struct UserView: View {
title: userDetails.person.displayName ?? userDetails.person.name,
subtitle: "@\(userDetails.person.name)@\(userDetails.person.actorId.host()!)",
avatarSubtext: $avatarSubtext,
avatarSubtextClicked: self.toggleCakeDayVisible,
bannerURL: shouldShowUserHeaders ? userDetails.person.banner : nil,
avatarUrl: userDetails.person.avatar,
avatarSubtextClicked: toggleCakeDayVisible,
bannerURL: shouldShowUserHeaders ? userDetails.person.bannerUrl : nil,
avatarUrl: userDetails.person.avatarUrl,
label1: "\(userDetails.counts.commentCount) Comments",
label2: "\(userDetails.counts.postCount) Posts")
}
Expand Down Expand Up @@ -443,14 +443,14 @@ struct UserViewPreview: PreviewProvider {
id: name.hashValue,
name: name,
displayName: displayName,
avatar: URL(string: "https://lemmy.ml/pictrs/image/df86c06d-341c-4e79-9c80-d7c7eb64967a.jpeg?format=webp"),
avatar: "https://lemmy.ml/pictrs/image/df86c06d-341c-4e79-9c80-d7c7eb64967a.jpeg?format=webp",
banned: false,
published: Date.now.advanced(by: -10000),
updated: nil,
actorId: URL(string: "https://google.com")!,
bio: "Just here for the good vibes!",
local: false,
banner: URL(string: "https://i.imgur.com/wcayaCB.jpeg"),
banner: "https://i.imgur.com/wcayaCB.jpeg",
deleted: false,
sharedInboxUrl: nil,
matrixUserId: nil,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ struct PostSettingsView: View {
WebsiteIconComplex(post: APIPost(
id: 0,
name: "",
url: URL(string: "https://lemmy.ml/post/1011734")!,
url: "https://lemmy.ml/post/1011734",
body: "",
creatorId: 0,
communityId: 0,
Expand All @@ -123,7 +123,7 @@ struct PostSettingsView: View {
nsfw: false,
published: .now,
removed: false,
thumbnailUrl: URL(string: "https://lemmy.ml/pictrs/image/1b759945-6651-497c-bee0-9bdb68f4a829.png"),
thumbnailUrl: "https://lemmy.ml/pictrs/image/1b759945-6651-497c-bee0-9bdb68f4a829.png",
updated: nil
))
.padding(.horizontal)
Expand Down
4 changes: 2 additions & 2 deletions MlemTests/Mocks/APICommunity+Mock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ extension APICommunity {
nsfw: Bool = false,
actorId: URL = URL(string: "https://mlem.group")!,
local: Bool = true,
icon: URL? = nil,
banner: URL? = nil,
icon: String? = nil,
banner: String? = nil,
hidden: Bool = false,
postingRestrictedToMods: Bool = false,
instanceId: Int = 0
Expand Down
Loading