aboutsummaryrefslogtreecommitdiff
path: root/src/utils
diff options
context:
space:
mode:
authorIvan Chen <ivan@tagg.id>2021-04-09 17:15:29 -0400
committerIvan Chen <ivan@tagg.id>2021-04-09 17:15:29 -0400
commit347e9e450268e4897b8dd241721b84945d9e2ec9 (patch)
tree58334be3724398c886365e99901e4442f5657172 /src/utils
parent097b515066f1a0c38cb7fb69cf78b16b945594e5 (diff)
parent3ec56863bfdd47b2ee8d0f0fe5a45be779508660 (diff)
Merge branch 'master' into tma756-bugfix-onpress-tagg-on-sp
# Conflicts: # src/components/taggs/TaggsBar.tsx
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/common.ts30
-rw-r--r--src/utils/layouts.ts1
-rw-r--r--src/utils/messages.ts83
-rw-r--r--src/utils/moments.ts20
4 files changed, 121 insertions, 13 deletions
diff --git a/src/utils/common.ts b/src/utils/common.ts
index 4f31af8e..0900a084 100644
--- a/src/utils/common.ts
+++ b/src/utils/common.ts
@@ -1,14 +1,14 @@
+import AsyncStorage from '@react-native-community/async-storage';
+import moment from 'moment';
+import {Linking} from 'react-native';
+import {getAll} from 'react-native-contacts';
+import {BROWSABLE_SOCIAL_URLS, TOGGLE_BUTTON_TYPE} from '../constants';
import {
ContactType,
NotificationType,
- UniversityType,
UniversityBadgeType,
+ UniversityType,
} from './../types/types';
-import moment from 'moment';
-import {Linking} from 'react-native';
-import {BROWSABLE_SOCIAL_URLS, TOGGLE_BUTTON_TYPE} from '../constants';
-import AsyncStorage from '@react-native-community/async-storage';
-import {getAll} from 'react-native-contacts';
export const getToggleButtonText: (
buttonType: string,
@@ -173,3 +173,21 @@ const _crestIcon = (university: UniversityType) => {
return require('../assets/images/bwbadges.png');
}
};
+
+export const createChannel = async (
+ loggedInUser: string,
+ id: string,
+ chatClient: any,
+) => {
+ console.log(loggedInUser, id, chatClient);
+ try {
+ const channel = chatClient.channel('messaging', {
+ members: [loggedInUser, id],
+ });
+ await channel.watch();
+ return channel;
+ } catch (error) {
+ console.log(error);
+ throw error;
+ }
+};
diff --git a/src/utils/layouts.ts b/src/utils/layouts.ts
index e2f1f0b1..4d0d557d 100644
--- a/src/utils/layouts.ts
+++ b/src/utils/layouts.ts
@@ -31,6 +31,7 @@ export const StatusBarHeight = Platform.select({
});
export const AvatarHeaderHeight = (HeaderHeight + StatusBarHeight) * 1.3;
+export const ChatHeaderHeight = (HeaderHeight + StatusBarHeight) * 1.1;
/**
* This is a function for normalizing the font size for different devices, based on iphone 8.
diff --git a/src/utils/messages.ts b/src/utils/messages.ts
new file mode 100644
index 00000000..d63f2b7a
--- /dev/null
+++ b/src/utils/messages.ts
@@ -0,0 +1,83 @@
+import moment from 'moment';
+import {RootState} from '../store/rootReducer';
+import {ChannelGroupedType} from '../types';
+
+/**
+ * Finds the difference in time in minutes
+ * @param lastActive given time e.g. "2021-04-08T19:07:09.361300983Z"
+ * @returns diff in minutes
+ */
+const _diffInMinutes = (lastActive: string | undefined) => {
+ if (!lastActive) {
+ return undefined;
+ }
+ return moment().diff(moment(lastActive), 'minutes');
+};
+
+/**
+ * Formats the last activity status.
+ * - "Active now" (≤ 5 minutes)
+ * - "Seen X minutes ago" (5 > x ≥ 59 minutes)
+ * - "Seen X hours ago" (x = [1, 2])
+ * - "Offline"
+ * @param lastActive given time e.g. "2021-04-08T19:07:09.361300983Z"
+ * @returns
+ */
+export const formatLastSeenText = (lastActive: string | undefined) => {
+ const diff = _diffInMinutes(lastActive);
+ if (!diff) {
+ return 'Offline';
+ }
+ if (diff <= 5) {
+ return 'Active now';
+ }
+ if (diff <= 59) {
+ return `Seen ${diff} minutes ago`;
+ }
+ if (diff <= 180) {
+ const hours = Math.floor(diff / 60);
+ return `Seen ${hours} hours ago`;
+ }
+ return 'Offline';
+};
+
+/**
+ * Checks if a lastActive timestamp is considered Online or not.
+ *
+ * A user is online if last active is ≤ 15 minutes.
+ *
+ * @param lastActive given time e.g. "2021-04-08T19:07:09.361300983Z"
+ * @returns True if active
+ */
+export const isOnline = (lastActive: string | undefined) => {
+ if (!lastActive) {
+ return false;
+ }
+ const diff = _diffInMinutes(lastActive);
+ if (!diff) {
+ return false;
+ }
+ return diff <= 15;
+};
+
+/**
+ * Gets the other member in the channel.
+ * @param channel the current chat channel
+ * @param state the current redux state
+ * @returns other member or undefined
+ */
+export const getMember = (
+ channel: ChannelGroupedType | undefined,
+ state: RootState,
+) => {
+ if (!channel) {
+ return undefined;
+ }
+ const loggedInUserId = state.user.user.userId;
+ const otherMembers = channel
+ ? Object.values(channel.state.members).filter(
+ (member) => member.user?.id !== loggedInUserId,
+ )
+ : [];
+ return otherMembers.length === 1 ? otherMembers[0] : undefined;
+};
diff --git a/src/utils/moments.ts b/src/utils/moments.ts
index 7428b1ac..87f062af 100644
--- a/src/utils/moments.ts
+++ b/src/utils/moments.ts
@@ -1,15 +1,17 @@
import moment from 'moment';
-//A util that calculates the difference between a given time and current time
-//Returns the difference in the largest possible unit of time (days > hours > minutes > seconds)
-
+/**
+ * Formats elapsed time from a given time.
+ * @param date_time given time
+ * @returns difference in the largest possible unit of time (days > hours > minutes > seconds)
+ */
export const getTimePosted = (date_time: string) => {
const datePosted = moment(date_time);
const now = moment();
var time = date_time;
var difference = now.diff(datePosted, 'seconds');
- //Creating elapsedTime string to display to user
+ // Creating elapsedTime string to display to user
// 0 to less than 1 minute
if (difference < 60) {
time = difference + ' seconds';
@@ -19,15 +21,19 @@ export const getTimePosted = (date_time: string) => {
difference = now.diff(datePosted, 'minutes');
time = difference + (difference === 1 ? ' minute' : ' minutes');
}
- //1 hour to less than 1 day
+ // 1 hour to less than 1 day
else if (difference >= 60 * 60 && difference < 24 * 60 * 60) {
difference = now.diff(datePosted, 'hours');
time = difference + (difference === 1 ? ' hour' : ' hours');
}
- //Any number of days
- else if (difference >= 24 * 60 * 60) {
+ // Any number of days
+ else if (difference >= 24 * 60 * 60 && difference < 24 * 60 * 60 * 3) {
difference = now.diff(datePosted, 'days');
time = difference + (difference === 1 ? ' day' : ' days');
}
+ // More than 3 days
+ else if (difference >= 24 * 60 * 60 * 3) {
+ time = datePosted.format('MMMM D, YYYY');
+ }
return time;
};