This repository has been archived by the owner on Oct 11, 2022. It is now read-only.
/
messages.js
257 lines (237 loc) · 6.91 KB
/
messages.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
//@flow
//$FlowFixMe
import database from 'firebase/database';
import { getStory } from './stories';
import { getMessageGroup } from './messageGroups';
import { getActiveMessageGroupObject } from '../actions/messageGroups';
import { createNotifications } from './notifications';
import { getUserInfo } from './users';
import { ACTIVITY_TYPES } from './types';
const UNIQUE = (v, i, a) => a.indexOf(v) === i;
type MessageTypes = 'messages' | 'messages_private';
type LocationTypes = 'stories' | 'message_groups';
export const getMessageKey = (type: MessageTypes): string =>
database().ref(type).push().key;
type GetMessageReturn = Promise<Object>;
export const getMessage = (
type: MessageTypes,
key: string
): GetMessageReturn => {
const db = database();
return db
.ref(`${type}/${key}`)
.once('value')
.then(snapshot => snapshot.val());
};
type GetMessagesFromLocationReturn = Promise<Array<Object>>;
export const getMessagesFromLocation = (
location: LocationTypes,
key: string
): GetMessagesFromLocationReturn => {
const getDataFunc = location === 'stories'
? getStory
: location === 'message_groups' ? getMessageGroup : getStory;
return getDataFunc(key).then(data => {
if (!data.messages) return Promise.resolve([]);
const messages = Object.keys(data.messages);
const type = location === 'stories'
? 'messages'
: location === 'message_groups' ? 'messages_private' : 'messages';
return Promise.all(messages.map(key => getMessage(type, key)));
});
return Promise.resolve([]);
};
type CreateMessageProps = {
storyId: string,
frequencyId: string,
user: Object,
message: {
type: string,
content: string,
},
key: ?string,
};
type CreateMessageReturn = Promise<Object>;
export const createMessage = ({
storyId,
frequencyId,
user,
message,
key,
}: CreateMessageProps): CreateMessageReturn => {
const db = database();
const messageType = 'messages';
const locationType = 'stories';
const id = key || getMessageKey(messageType);
return db
.ref()
.update({
[`${messageType}/${id}`]: {
id,
storyId,
frequencyId,
timestamp: database.ServerValue.TIMESTAMP,
userId: user.uid,
message,
},
[`${locationType}/${storyId}/last_activity`]: database.ServerValue
.TIMESTAMP,
[`${locationType}/${storyId}/participants/${user.uid}`]: {
id: user.uid,
last_activity: database.ServerValue.TIMESTAMP,
},
[`${locationType}/${storyId}/messages/${id}`]: {
id,
},
})
.then(() => db.ref(`${locationType}/${storyId}`).once('value'))
.then(snapshot => snapshot.val())
.then(story => {
// If this is a new story after the intro of story.participants, use that rather than
// getting them manually
if (story.participants) return Object.keys(story.participants);
// If this is an old story without story.participants, get all the messages
// and manually get all users that sent messages
return getMessagesFromLocation(locationType, storyId).then(messages =>
messages
// - Everybody who's sent a message in that story before
.map(({ userId }) => userId)
// - Creator of story
.concat([story.creator.uid])
.filter(UNIQUE)
);
})
.then(users =>
createNotifications({
// Avoid notifying the sender
users: users.filter(uid => uid !== user.uid),
activityType: ACTIVITY_TYPES.NEW_MESSAGE,
ids: {
frequency: frequencyId,
story: storyId,
},
sender: {
uid: user.uid,
displayName: user.displayName,
photoURL: user.photoURL,
},
content: message.type === 'text' ? message.content.substr(0, 140) : '',
})
)
.then(() => db.ref(`${messageType}/${id}`).once('value'))
.then(snapshot => snapshot.val());
};
type CreatePrivateMessageProps = {
messageGroupId: string,
userId: string,
message: {
type: string,
content: string,
},
key: ?string,
};
type CreatePrivateMessageReturn = Promise<Object>;
export const createPrivateMessage = ({
messageGroupId,
userId,
message,
key,
}: CreatePrivateMessageProps): CreatePrivateMessageReturn => {
const db = database();
const messageType = 'messages_private';
const locationType = 'message_groups';
const id = key || getMessageKey(messageType);
return db
.ref()
.update({
[`${messageType}/${id}`]: {
id,
messageGroupId,
timestamp: database.ServerValue.TIMESTAMP,
userId,
message,
},
[`${locationType}/${messageGroupId}/last_activity`]: database.ServerValue
.TIMESTAMP,
[`${locationType}/${messageGroupId}/users/${userId}/last_activity`]: database
.ServerValue.TIMESTAMP,
[`${locationType}/${messageGroupId}/messages/${id}`]: {
id,
},
[`${locationType}/${messageGroupId}/snippet`]: message.type === 'text'
? message.content
: 'Sent an attachment', // save a snippet of the last text message, or assume a media message
})
.then(() => db.ref(`${messageType}/${id}`).once('value'))
.then(snapshot => snapshot.val());
};
export const setLastActivityOnMessageGroupParticipants = (
messageGroup: Object
) => {
const users = Object.keys(messageGroup.users);
Promise.all(
users.map(user => setLastActiveOnMessageGroupForUser(messageGroup.id, user))
);
};
export const setLastActiveOnMessageGroupForUser = (
messageGroup: string,
user: string
) => {
const db = database();
return db.ref(`/users/${user}/messageGroups/${messageGroup}`).update({
['last_activity']: database.ServerValue.TIMESTAMP,
});
};
type CreateReactionProps = {
type: MessageTypes,
messageId: string,
uid: string,
};
export const createReaction = ({
type,
messageId,
uid,
}: CreateReactionProps) => {
const db = database();
const TYPE = 'like';
return db
.ref()
.update({
[`${type}/${messageId}/reactions/${uid}`]: {
type: TYPE,
timestamp: database.ServerValue.TIMESTAMP,
},
})
.then(() => Promise.all([getMessage(type, messageId), getUserInfo(uid)]))
.then(([message, user]) => {
// no notifications needed for private message reactions
if (type === 'messages_private') return;
createNotifications({
users: [message.userId],
activityType: ACTIVITY_TYPES.REACTION,
ids: {
frequency: message.frequencyId,
story: message.storyId,
},
sender: {
uid,
displayName: user.displayName,
photoURL: user.photoURL,
},
content: TYPE,
});
});
};
type DeleteReactionProps = {
type: MessageTypes,
messageId: string,
uid: string,
};
export const deleteReaction = ({
type,
messageId,
uid,
}: DeleteReactionProps) => {
const db = database();
return db.ref(`/${type}/${messageId}/reactions/${uid}`).remove();
};