Skip to content

Commit

Permalink
Release 1.5.7 (229)
Browse files Browse the repository at this point in the history
  • Loading branch information
denis15yo committed Mar 11, 2024
1 parent 1f54a12 commit 4783892
Show file tree
Hide file tree
Showing 436 changed files with 27,691 additions and 5,182 deletions.
27 changes: 27 additions & 0 deletions Nicegram/NGLottie/Sources/LottieViewImpl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@ extension LottieViewImpl: LottieViewProtocol {
)
}

public func setImageProvider(_ imageProvider: ImageProvider?) {
if let imageProvider {
animationView.imageProvider = AnonymousImageProvider(
provider: imageProvider
)
} else {
animationView.imageProvider = BundleImageProvider(
bundle: .main,
searchPath: nil
)
}
}

public func setLoopMode(_ loopMode: LoopMode) {
let mode: LottieLoopMode
switch loopMode {
Expand All @@ -62,3 +75,17 @@ extension LottieViewImpl: LottieViewProtocol {
animationView.stop()
}
}

private class AnonymousImageProvider {
let provider: LottieViewProtocol.ImageProvider

init(provider: LottieViewProtocol.ImageProvider) {
self.provider = provider
}
}

extension AnonymousImageProvider: AnimationImageProvider {
func imageForAsset(asset: ImageAsset) -> CGImage? {
provider.image(asset.id)
}
}
4 changes: 1 addition & 3 deletions Nicegram/NGStats/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ swift_library(
"Sources/**/*.swift",
]),
deps = [
"@swiftpkg_nicegram_assistant_ios//:NGApi",
"@swiftpkg_nicegram_assistant_ios//:FeatNicegramHub",
"//submodules/AccountContext:AccountContext",
"//Nicegram/NGData:NGData",
"//Nicegram/NGRemoteConfig:NGRemoteConfig",
"//Nicegram/NGUtils:NGUtils",
],
visibility = [
Expand Down
278 changes: 278 additions & 0 deletions Nicegram/NGStats/Sources/ChatsSharing.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,278 @@
import AccountContext
import FeatNicegramHub
import Foundation
import NGUtils
import Postbox
import SwiftSignalKit
import TelegramCore

@available(iOS 13.0, *)
public func sharePeerData(
peerId: PeerId,
context: AccountContext
) {
_ = (context.account.viewTracker.peerView(peerId, updateData: true)
|> take(1))
.start(next: { peerView in
if let peer = peerView.peers[peerView.peerId] {
Task {
await sharePeerData(
peer: peer,
cachedData: peerView.cachedData,
context: context
)
}
}
})
}

@available(iOS 13.0, *)
private func sharePeerData(
peer: Peer,
cachedData: CachedPeerData?,
context: AccountContext
) async {
let peerId = extractPeerId(peer: peer)

let type: PeerType
switch EnginePeer(peer) {
case let .channel(channel):
switch channel.info {
case .group:
type = .group
case .broadcast:
type = .channel
}
case .legacyGroup:
type = .group
case .user(let user):
if user.botInfo != nil {
type = .bot
} else {
return
}
default:
return
}

let sharePeerDataUseCase = NicegramHubContainer.shared.sharePeerDataUseCase()

await sharePeerDataUseCase(
id: peerId,
participantsCount: extractParticipantsCount(
peer: peer,
cachedData: cachedData
),
type: type,
peerDataProvider: {
await withCheckedContinuation { continuation in
let avatarImageSignal = fetchAvatarImage(peer: peer, context: context)
let inviteLinksSignal = context.engine.peers.direct_peerExportedInvitations(peerId: peer.id, revoked: false)
let interlocutorLanguageSignal = wrapped_detectInterlocutorLanguage(forChatWith: peer.id, context: context)

_ = (combineLatest(avatarImageSignal, inviteLinksSignal, interlocutorLanguageSignal)
|> take(1)).start(next: { avatarImageData, inviteLinks, interlocutorLanguage in
let peerData = PeerData(
avatarImageData: avatarImageData?.base64EncodedString(),
id: peerId,
inviteLinks: extractInviteLinks(inviteLinks),
payload: extractPayload(
peer: peer,
cachedData: cachedData,
interlocutorLanguage: interlocutorLanguage
),
type: type
)
continuation.resume(returning: peerData)
})
}
}
)
}

private func extractPayload(
peer: Peer,
cachedData: CachedPeerData?,
interlocutorLanguage: String?
) -> PeerPayload? {
switch EnginePeer(peer) {
case let .legacyGroup(group):
PeerPayload.legacyGroup(
extractPayload(
group: group,
cachedData: cachedData as? CachedGroupData,
lastMessageLanguageCode: interlocutorLanguage
)
)
case let .channel(channel):
PeerPayload.channel(
extractPayload(
channel: channel,
cachedData: cachedData as? CachedChannelData,
lastMessageLanguageCode: interlocutorLanguage
)
)
case let .user(user):
if let botInfo = user.botInfo {
PeerPayload.user(
extractPayload(
bot: user,
botInfo: botInfo,
lastMessageLanguageCode: interlocutorLanguage
)
)
} else {
nil
}
default:
nil
}
}

private func extractPayload(group: TelegramGroup, cachedData: CachedGroupData?, lastMessageLanguageCode: String?) -> LegacyGroupPayload {
LegacyGroupPayload(
deactivated: group.flags.contains(.deactivated),
title: group.title,
participantsCount: group.participantCount,
date: group.creationDate,
migratedTo: group.migrationReference?.peerId.id._internalGetInt64Value(),
photo: extractChatPhoto(peer: group),
lastMessageLang: lastMessageLanguageCode,
about: cachedData?.about
)
}

private func extractPayload(channel: TelegramChannel, cachedData: CachedChannelData?, lastMessageLanguageCode: String?) -> ChannelPayload {
ChannelPayload(
verified: channel.isVerified,
scam: channel.isScam,
hasGeo: channel.flags.contains(.hasGeo),
fake: channel.isFake,
gigagroup: channel.flags.contains(.isGigagroup),
title: channel.title,
username: channel.username,
date: channel.creationDate,
restrictions: extractRestrictions(restrictionInfo: channel.restrictionInfo),
participantsCount: cachedData?.participantsSummary.memberCount,
photo: extractChatPhoto(peer: channel),
lastMessageLang: lastMessageLanguageCode,
about: cachedData?.about,
geoLocation: cachedData?.peerGeoLocation.map {
extractGeoLocation($0)
}
)
}

private func extractPayload(
bot: TelegramUser,
botInfo: BotUserInfo,
lastMessageLanguageCode: String?
) -> UserPayload {
let botFlags = botInfo.flags
let userFlags = bot.flags

return UserPayload(
deleted: bot.isDeleted,
bot: true,
botChatHistory: botFlags.contains(.hasAccessToChatHistory),
botNochats: !botFlags.contains(.worksWithGroups),
verified: bot.isVerified,
restricted: bot.restrictionInfo != nil,
botInlineGeo: botFlags.contains(.requiresGeolocationForInlineRequests),
support: userFlags.contains(.isSupport),
scam: bot.isScam,
fake: bot.isFake,
botAttachMenu: botFlags.contains(.canBeAddedToAttachMenu),
premium: bot.isPremium,
botCanEdit: botFlags.contains(.canEdit),
firstName: bot.firstName,
lastName: bot.lastName,
username: bot.username,
phone: bot.phone,
photo: extractUserProfilePhoto(peer: bot),
restrictionReason: extractRestrictions(restrictionInfo: bot.restrictionInfo),
botInlinePlaceholder: botInfo.inlinePlaceholder,
langCode: lastMessageLanguageCode,
usernames: bot.usernames.map {
.init(
editable: $0.flags.contains(.isEditable),
active: $0.isActive,
username: $0.username
)
}
)
}

private func extractChatPhoto(
peer: Peer
) -> ChatPhoto? {
guard let imageRepresentation = peer.profileImageRepresentations.first else {
return nil
}
guard let resource = imageRepresentation.resource as? CloudPeerPhotoSizeMediaResource else {
return nil
}

return ChatPhoto(mediaResourceId: resource.id.stringRepresentation, datacenterId: resource.datacenterId, photoId: resource.photoId, volumeId: resource.volumeId, localId: resource.localId, sizeSpec: resource.sizeSpec.rawValue)
}

private func extractUserProfilePhoto(
peer: Peer
) -> UserProfilePhoto? {
guard let imageRepresentation = peer.profileImageRepresentations.first else {
return nil
}
guard let resource = imageRepresentation.resource as? CloudPeerPhotoSizeMediaResource else {
return nil
}

return UserProfilePhoto(
hasVideo: imageRepresentation.hasVideo,
personal: imageRepresentation.isPersonal,
photoId: resource.photoId,
dcId: resource.datacenterId
)
}

func extractParticipantsCount(peer: Peer, cachedData: CachedPeerData?) -> Int {
switch EnginePeer(peer) {
case .user:
return 2
case .channel:
let channelData = cachedData as? CachedChannelData
return Int(channelData?.participantsSummary.memberCount ?? 0)
case let .legacyGroup(group):
return group.participantCount
case .secretChat:
return 0
}
}

private func extractRestrictions(
restrictionInfo: PeerAccessRestrictionInfo?
) -> [FeatNicegramHub.RestrictionRule] {
restrictionInfo?.rules.map {
FeatNicegramHub.RestrictionRule(
platform: $0.platform,
reason: $0.reason,
text: $0.text
)
} ?? []
}

private func extractGeoLocation(
_ geo: PeerGeoLocation
) -> GeoLocation {
GeoLocation(latitude: geo.latitude, longitude: geo.longitude, address: geo.address)
}

private func extractInviteLinks(_ links: ExportedInvitations?) -> [InviteLink]? {
links?.list?.compactMap { link in
switch link {
case let .link(link, title, isPermanent, requestApproval, isRevoked, adminId, date, startDate, expireDate, usageLimit, count, requestedCount):
InviteLink(link: link, title: title, isPermanent: isPermanent, requestApproval: requestApproval, isRevoked: isRevoked, adminId: adminId.id._internalGetInt64Value(), date: date, startDate: startDate, expireDate: expireDate, usageLimit: usageLimit, count: count, requestedCount: requestedCount)
case .publicJoinRequest:
nil
}
}
}
Loading

0 comments on commit 4783892

Please sign in to comment.