Skip to content

Commit

Permalink
Merge pull request #3970 from tloncorp/hm/fix-missing-group-invite-no…
Browse files Browse the repository at this point in the history
…tifs

notifications: get group invites working as pushes
  • Loading branch information
arthyn authored Oct 1, 2024
2 parents 9cc3bc5 + 588a15c commit 2542c80
Show file tree
Hide file tree
Showing 13 changed files with 153 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ public void fetchGroups(TalkObjectCallback callback) {
fetchObject("/~/scry/groups/groups/light", callback);
}

public void fetchGangs(TalkObjectCallback callback) {
fetchObject("/~/scry/groups/gangs", callback);
}

public void fetchGroupChannel(String channelId, TalkObjectCallback callback) {
fetchGroups(new TalkObjectCallback() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Optional;

public class Yarn {

public Boolean isValidNotification;
public Boolean isGroup;
public Boolean isClub;
public String wer;
public String channelId;
public Optional<String> channelId;
public Optional<String> groupId;
public String senderId;
public String contentText;

Expand Down Expand Up @@ -62,18 +64,22 @@ public Yarn(JSONObject yarnObject) throws JSONException {

isGroup = !rope.isNull("group");
isValidNotification = rope.getString("desk").equals("groups") &&
!thread.endsWith("/channel/edit") &&
(!thread.endsWith("/channel/edit") &&
!thread.endsWith("/channel/add") &&
!thread.endsWith("/channel/del") &&
!thread.endsWith("/joins") &&
!thread.endsWith("/leaves") &&
(!rope.isNull("channel") || !isGroup);
!thread.endsWith("/leaves") ||
!isGroup);
wer = yarnObject.getString("wer");
channelId = isGroup ? rope.getString("channel") : thread.replace("/dm/", "").replace("/club/", "");
isClub = channelId.startsWith("0v");
groupId = !isGroup ? Optional.empty() :
Optional.of(rope.getString("group"));
channelId = isGroup ?
rope.isNull("channel") ? Optional.empty() : Optional.of(rope.getString("channel"))
: Optional.of(thread.replace("/dm/", "").replace("/club/", ""));
isClub = channelId.isPresent() && channelId.get().startsWith("0v");
contentText = String.join("", parts);
if (senderId == null && channelId.startsWith("~")) {
senderId = channelId;
if (senderId == null && channelId.isPresent() && channelId.get().startsWith("~")) {
senderId = channelId.get();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.json.JSONObject;

import java.util.Date;
import java.util.Iterator;

import io.invertase.firebase.crashlytics.ReactNativeFirebaseCrashlyticsNativeHelper;
import io.tlon.landscape.AppLifecycleManager;
Expand Down Expand Up @@ -75,10 +76,11 @@ public void onComplete(JSONObject response) {
@Override
public void onComplete(JSONObject response) {
final Contact contact = new Contact(yarn.senderId, response);
final String channelId = yarn.channelId.orElse("");
createNotificationTitle(api, yarn, contact, title -> {
Bundle data = new Bundle();
data.putString("wer", yarn.wer);
data.putString("channelId", yarn.channelId);
data.putString("channelId", channelId);
data.putInt("notificationId", id);
sendNotification(
context,
Expand Down Expand Up @@ -109,9 +111,9 @@ public void onError(VolleyError error) {
}

private static void createNotificationTitle(TalkApi api, Yarn yarn, Contact contact, TalkNotificationContentCallback callback) {
if (yarn.isGroup) {
final String fallbackTitle = yarn.channelId.replace("chat/", "");
api.fetchGroupChannel(yarn.channelId, new TalkObjectCallback() {
if (yarn.isGroup && yarn.channelId.isPresent()) {
final String fallbackTitle = yarn.channelId.get().replace("chat/", "");
api.fetchGroupChannel(yarn.channelId.get(), new TalkObjectCallback() {
@Override
public void onComplete(JSONObject response) {
try {
Expand All @@ -131,18 +133,49 @@ public void onError(VolleyError error) {
return;
}

if (yarn.isClub) {
api.fetchClub(yarn.channelId, new TalkObjectCallback() {
if (yarn.isGroup && yarn.groupId.isPresent() && !yarn.channelId.isPresent()) {
// this only works for group invites, if we start handling other events, we need to
// also fetch groups and prioritize that metadata over gangs
api.fetchGangs(new TalkObjectCallback() {
final String groupId = yarn.groupId.get();
@Override
public void onComplete(JSONObject response) {
Club club = new Club(yarn.channelId, response);
try {
JSONObject group = response.getJSONObject(groupId);
if (group.isNull("preview")) {
callback.onComplete(groupId);
return;
}

String title = group.getJSONObject("preview").getJSONObject("meta").getString("title");
callback.onComplete(title.isEmpty() ? groupId : title);
} catch (JSONException e) {
callback.onComplete(groupId);
}
}

@Override
public void onError(VolleyError error) {
ReactNativeFirebaseCrashlyticsNativeHelper.recordNativeException(new Exception("notifications_fetch_groups", error));
callback.onComplete(groupId);
}
});

return;
}

if (yarn.isClub && yarn.channelId.isPresent()) {
api.fetchClub(yarn.channelId.get(), new TalkObjectCallback() {
@Override
public void onComplete(JSONObject response) {
Club club = new Club(yarn.channelId.get(), response);
callback.onComplete(club.displayName);
}

@Override
public void onError(VolleyError error) {
ReactNativeFirebaseCrashlyticsNativeHelper.recordNativeException(new Exception("notifications_fetch_club", error));
callback.onComplete(yarn.channelId);
callback.onComplete(yarn.channelId.get());
}
});
return;
Expand Down
24 changes: 18 additions & 6 deletions apps/tlon-mobile/ios/Landscape.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
3DC46BC1B669DF8F5F059CA8 /* ExpoModulesProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8ACB38DC4FF4D6377774BF5F /* ExpoModulesProvider.swift */; };
3E461D99554A48A4959DE609 /* SplashScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = AA286B85B6C04FC6940260E9 /* SplashScreen.storyboard */; };
4ACCD7281520ED84DDC2759A /* PrivacyInfo.xcprivacy in Resources */ = {isa = PBXBuildFile; fileRef = 74D2BEF1344CF71A07C17F5E /* PrivacyInfo.xcprivacy */; };
5A1EB9532CAC451A00029EDC /* GroupStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5A1EB9522CAC451A00029EDC /* GroupStore.swift */; };
5A1EB9542CAC451A00029EDC /* GroupStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5A1EB9522CAC451A00029EDC /* GroupStore.swift */; };
5A1EB9552CAC451A00029EDC /* GroupStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5A1EB9522CAC451A00029EDC /* GroupStore.swift */; };
5A1EB9562CAC451A00029EDC /* GroupStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5A1EB9522CAC451A00029EDC /* GroupStore.swift */; };
5A1EB9572CAC451A00029EDC /* GroupStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5A1EB9522CAC451A00029EDC /* GroupStore.swift */; };
630DE0B72C51A0F80053603B /* Error+isAFTimeout.swift in Sources */ = {isa = PBXBuildFile; fileRef = 632793BD2C4AE4B500F942B1 /* Error+isAFTimeout.swift */; };
630DE0C52C51A8780053603B /* UIColor+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 70D386702A60A3E600AFB46E /* UIColor+Extension.swift */; };
630DE0C62C51A8780053603B /* Contact.swift in Sources */ = {isa = PBXBuildFile; fileRef = 700B635C2A71DFE90017F40F /* Contact.swift */; };
Expand Down Expand Up @@ -225,6 +230,7 @@
13B07FB61A68108700A75B9A /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = Info.plist; path = Landscape/Info.plist; sourceTree = "<group>"; };
13B07FB71A68108700A75B9A /* main.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = main.m; path = Landscape/main.m; sourceTree = "<group>"; };
3B0B210E999A7E0E1A345468 /* Pods-Landscape-preview.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Landscape-preview.release.xcconfig"; path = "Target Support Files/Pods-Landscape-preview/Pods-Landscape-preview.release.xcconfig"; sourceTree = "<group>"; };
5A1EB9522CAC451A00029EDC /* GroupStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GroupStore.swift; sourceTree = "<group>"; };
630DE0B82C51A1500053603B /* LandscapePreview-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = "LandscapePreview-Bridging-Header.h"; path = "Landscape/LandscapePreview-Bridging-Header.h"; sourceTree = "<group>"; };
630DE0E22C51A8790053603B /* Notifications-preview.appex */ = {isa = PBXFileReference; explicitFileType = "wrapper.app-extension"; includeInIndex = 0; path = "Notifications-preview.appex"; sourceTree = BUILT_PRODUCTS_DIR; };
630DE0EB2C51AC840053603B /* UserDefaults+appGroup.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UserDefaults+appGroup.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -269,11 +275,11 @@
70F99A8E2B2D2B5700D77256 /* LandscapeTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = LandscapeTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
70F99A972B2D2B6E00D77256 /* YarnTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = YarnTests.swift; sourceTree = "<group>"; };
74633B6584F10A6AA2FEE339 /* Pods-Landscape.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Landscape.release.xcconfig"; path = "Target Support Files/Pods-Landscape/Pods-Landscape.release.xcconfig"; sourceTree = "<group>"; };
74D2BEF1344CF71A07C17F5E /* PrivacyInfo.xcprivacy */ = {isa = PBXFileReference; includeInIndex = 1; name = PrivacyInfo.xcprivacy; path = Landscape/PrivacyInfo.xcprivacy; sourceTree = "<group>"; };
74D2BEF1344CF71A07C17F5E /* PrivacyInfo.xcprivacy */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xml; name = PrivacyInfo.xcprivacy; path = Landscape/PrivacyInfo.xcprivacy; sourceTree = "<group>"; };
79BDD5DCE15A385BB361AED1 /* Pods_Landscape.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Landscape.framework; sourceTree = BUILT_PRODUCTS_DIR; };
7A4D352CD337FB3A3BF06240 /* Pods-Landscape.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Landscape.release.xcconfig"; path = "Target Support Files/Pods-Landscape/Pods-Landscape.release.xcconfig"; sourceTree = "<group>"; };
8ACB38DC4FF4D6377774BF5F /* ExpoModulesProvider.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = ExpoModulesProvider.swift; path = "Pods/Target Support Files/Pods-Landscape-preview/ExpoModulesProvider.swift"; sourceTree = "<group>"; };
9D6B23EFDEDD5888235A22BB /* PrivacyInfo.xcprivacy */ = {isa = PBXFileReference; includeInIndex = 1; name = PrivacyInfo.xcprivacy; path = Landscape/PrivacyInfo.xcprivacy; sourceTree = "<group>"; };
9D6B23EFDEDD5888235A22BB /* PrivacyInfo.xcprivacy */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xml; name = PrivacyInfo.xcprivacy; path = Landscape/PrivacyInfo.xcprivacy; sourceTree = "<group>"; };
AA286B85B6C04FC6940260E9 /* SplashScreen.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; name = SplashScreen.storyboard; path = Landscape/SplashScreen.storyboard; sourceTree = "<group>"; };
B35AC6CF758CAFC9D112A69F /* Pods-Landscape.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Landscape.debug.xcconfig"; path = "Target Support Files/Pods-Landscape/Pods-Landscape.debug.xcconfig"; sourceTree = "<group>"; };
BB2F792C24A3F905000567C9 /* Expo.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Expo.plist; sourceTree = "<group>"; };
Expand Down Expand Up @@ -438,6 +444,7 @@
6374ACF82C4ACD5000E637C0 /* LoginStore.swift */,
700B635F2A71E0810017F40F /* SettingsStore.swift */,
7036E3532ACD0FC90020A9FB /* UserDefaultsStore.swift */,
5A1EB9522CAC451A00029EDC /* GroupStore.swift */,
);
path = Storage;
sourceTree = "<group>";
Expand Down Expand Up @@ -1180,6 +1187,7 @@
7083A16C2AFB01A30022404A /* TlonError.swift in Sources */,
632793BE2C4AE4B500F942B1 /* Error+isAFTimeout.swift in Sources */,
7036E3542ACD0FC90020A9FB /* UserDefaultsStore.swift in Sources */,
5A1EB9532CAC451A00029EDC /* GroupStore.swift in Sources */,
70D3865A2A609BFC00AFB46E /* PocketNotificationsAPI.swift in Sources */,
C83014822C7BA74C00D9A5CA /* UIFont+SystemDesign.m in Sources */,
70D3865B2A609BFC00AFB46E /* PocketAPI.swift in Sources */,
Expand Down Expand Up @@ -1226,6 +1234,7 @@
63E27E0E2C5AF26C008ACB45 /* Alamofire+sessionWithSharedCookieStorage.swift in Sources */,
630DE0CF2C51A8780053603B /* PocketUserAPI.swift in Sources */,
630DE0D02C51A8780053603B /* UserDefaultsStore.swift in Sources */,
5A1EB9572CAC451A00029EDC /* GroupStore.swift in Sources */,
630DE0D12C51A8780053603B /* ContactStore.swift in Sources */,
630DE0D22C51A8780053603B /* NotificationService.swift in Sources */,
630DE0D32C51A8780053603B /* ClubStore.swift in Sources */,
Expand Down Expand Up @@ -1257,6 +1266,7 @@
63E27E0D2C5AF26C008ACB45 /* Alamofire+sessionWithSharedCookieStorage.swift in Sources */,
632793C62C4AE56F00F942B1 /* PocketUserAPI.swift in Sources */,
632793C42C4AE53A00F942B1 /* UserDefaultsStore.swift in Sources */,
5A1EB9562CAC451A00029EDC /* GroupStore.swift in Sources */,
632793C32C4AE53500F942B1 /* ContactStore.swift in Sources */,
6374ACE22C49DA9600E637C0 /* NotificationService.swift in Sources */,
632793CB2C4AE7D900F942B1 /* ClubStore.swift in Sources */,
Expand All @@ -1280,6 +1290,7 @@
70DBBFEC2B7C60B50021EA96 /* TlonError.swift in Sources */,
632793BF2C4AE4B500F942B1 /* Error+isAFTimeout.swift in Sources */,
70DBBFED2B7C60B50021EA96 /* UserDefaultsStore.swift in Sources */,
5A1EB9542CAC451A00029EDC /* GroupStore.swift in Sources */,
70DBBFEE2B7C60B50021EA96 /* PocketNotificationsAPI.swift in Sources */,
70DBBFEF2B7C60B50021EA96 /* PocketAPI.swift in Sources */,
70DBBFF02B7C60B50021EA96 /* PushNotificationManager.swift in Sources */,
Expand Down Expand Up @@ -1326,6 +1337,7 @@
70F99AA92B2D337600D77256 /* GroupChannelStore.swift in Sources */,
70F99A9F2B2D336800D77256 /* Group.swift in Sources */,
70F99AA32B2D336E00D77256 /* PocketUserAPI.swift in Sources */,
5A1EB9552CAC451A00029EDC /* GroupStore.swift in Sources */,
70F99AA22B2D336E00D77256 /* UrbitAPI.swift in Sources */,
70F99AAA2B2D337600D77256 /* SettingsStore.swift in Sources */,
70F99AA62B2D336E00D77256 /* PocketChatAPI.swift in Sources */,
Expand Down Expand Up @@ -1467,7 +1479,7 @@
INFOPLIST_FILE = "Notifications/Info-preview.plist";
INFOPLIST_KEY_CFBundleDisplayName = "Notifications-preview";
INFOPLIST_KEY_NSHumanReadableCopyright = "";
IPHONEOS_DEPLOYMENT_TARGET = 17.0;
IPHONEOS_DEPLOYMENT_TARGET = 16.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down Expand Up @@ -1512,7 +1524,7 @@
INFOPLIST_FILE = "Notifications/Info-preview.plist";
INFOPLIST_KEY_CFBundleDisplayName = "Notifications-preview";
INFOPLIST_KEY_NSHumanReadableCopyright = "";
IPHONEOS_DEPLOYMENT_TARGET = 17.0;
IPHONEOS_DEPLOYMENT_TARGET = 16.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down Expand Up @@ -1558,7 +1570,7 @@
INFOPLIST_FILE = Notifications/Info.plist;
INFOPLIST_KEY_CFBundleDisplayName = Notifications;
INFOPLIST_KEY_NSHumanReadableCopyright = "";
IPHONEOS_DEPLOYMENT_TARGET = 17.0;
IPHONEOS_DEPLOYMENT_TARGET = 16.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down Expand Up @@ -1603,7 +1615,7 @@
INFOPLIST_FILE = Notifications/Info.plist;
INFOPLIST_KEY_CFBundleDisplayName = Notifications;
INFOPLIST_KEY_NSHumanReadableCopyright = "";
IPHONEOS_DEPLOYMENT_TARGET = 17.0;
IPHONEOS_DEPLOYMENT_TARGET = 16.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down
18 changes: 6 additions & 12 deletions apps/tlon-mobile/ios/Landscape/AppDelegate.mm
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
// You can add your custom initial props in the dictionary below.
// They will be passed down to the ViewController used by React Native.
self.initialProps = @{};

[PushNotificationManager configure];

#if PREVIEW
[RNBranch useTestInstance];
#endif

[RNBranch initSessionWithLaunchOptions:launchOptions isReferrable:YES];

// Listen to changes in app-specific cookie storage, and push those to the app group shared
// storage.
// Ideally, we'd exclusively use the app group storage for all cookie read/writes, but I could
Expand Down Expand Up @@ -60,7 +60,7 @@ - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url options:(N
if ([RNBranch application:application openURL:url options:options]) {
return YES;
}

return [super application:application openURL:url options:options] || [RCTLinkingManager application:application openURL:url options:options];
}

Expand All @@ -70,7 +70,7 @@ - (BOOL)application:(UIApplication *)application continueUserActivity:(nonnull N
if ([RNBranch continueUserActivity:userActivity]) {
return YES;
}

BOOL result = [RCTLinkingManager application:application continueUserActivity:userActivity restorationHandler:restorationHandler];
return [super application:application continueUserActivity:userActivity restorationHandler:restorationHandler] || result;
}
Expand All @@ -87,10 +87,4 @@ - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotif
return [super application:application didFailToRegisterForRemoteNotificationsWithError:error];
}

// Explicitly define remote notification delegates to ensure compatibility with some third-party libraries
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
return [PushNotificationManager handleBackgroundNotification:userInfo completionHandler:completionHandler];
}

@end
8 changes: 8 additions & 0 deletions apps/tlon-mobile/ios/Shared/Models/Group.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,11 @@ struct Group: Codable {
let channels: [String: GroupChannel]
let meta: GroupMeta
}

struct Gang: Codable {
let preview: GangPreview?
}

struct GangPreview: Codable {
let meta: GroupMeta
}
1 change: 1 addition & 0 deletions apps/tlon-mobile/ios/Shared/Models/TlonError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ enum TlonError {
static var NotificationsFetchContacts = "notifications_fetch_contacts"
static var NotificationsShowBanner = "notifications_show_banner"
static var NotificationsShowFallback = "notifications_show_fallback"
static var NotificationsFetchGangs = "notifications_fetch_gangs"
}
Loading

0 comments on commit 2542c80

Please sign in to comment.