Skip to content

Commit

Permalink
Add capability to read custom nicknames from new thread data.
Browse files Browse the repository at this point in the history
  • Loading branch information
marcel-valdez committed Feb 13, 2018
1 parent eecd5fa commit 6da6582
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions scripts/messenger.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,6 @@
const request = require('request'); // For making HTTP requests
const Settings = require('./settings');

function getThreadName(thread, participant) {
if (!Settings.properties.useCustomNicknames) {
return participant.name;
}

const nicknames = thread['custom_nickname'];
return (nicknames && nicknames[participant['fbid']]) || participant.name;
}

class Messenger {

Expand Down Expand Up @@ -64,6 +56,23 @@ class Messenger {
}
}

getUserDisplayName(userId) {
if (!Settings.properties.useCustomNicknames) {
return this.users[userId].name;
}

return this.users[userId].custom_nickname
|| this.users[userId].name;
}


saveParticipantCustomizations(customizations) {
customizations
.map(({ participant_id: id, nickname }) =>
[ id, id !== this.userId ? nickname : 'Me' ])
.map(args => this.setCustomNickname(...args));
}

// Parses a list of conversation participants into users
// Useful for adding users that are not "friends" to our database
saveParticipantsAsFriends(participants) {
Expand Down Expand Up @@ -385,6 +394,7 @@ class Messenger {
// Get name from convo participants
const participants = thread.all_participants.nodes;
const otherParticipants = participants.filter(participant => { return participant.messaging_actor.id !== this.userId;});
// TODO: Use nicknames or short names?
let threadName = otherParticipants.reduce((reducer, participant) => {
return `${reducer}${participant.messaging_actor.short_name}, `;
}, '');
Expand All @@ -411,8 +421,9 @@ class Messenger {
} else {
// No nicknames for now
for (const participant of thread.all_participants.nodes) {
if (participant.messaging_actor.id === id)
name = participant.messaging_actor.name;
if (participant.messaging_actor.id === id) {
name = this.getUserDisplayName(id);
}
}
}

Expand Down Expand Up @@ -476,6 +487,11 @@ class Messenger {
}

for (const thread of threads) {
if (thread.customization_info &&
thread.customization_info.participant_customizations) {
this.saveParticipantCustomizations(
thread.customization_info.participant_customizations);
}
this.saveParticipantsAsFriends(thread.all_participants);
}

Expand Down

0 comments on commit 6da6582

Please sign in to comment.