Skip to content

Commit

Permalink
🔀 Merge branch 'master' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
williamchong committed Feb 17, 2023
2 parents 7000151 + 2580653 commit 97b2cb5
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 26 deletions.
55 changes: 46 additions & 9 deletions src/pages/writing-nft/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,19 @@
<script>
import { LIKECOIN_NFT_CAMPAIGN_ITEMS } from '~/constant';
import { getLatestNFTClasses, getTopNFTClasses } from '~/util/api';
import {
getLatestNFTClasses,
getTopNFTClasses,
getISCNRecord,
} from '~/util/api';
import { logTrackerEvent } from '~/util/EventLogger';
import navigationListenerMixin from '~/mixins/navigation-listener';
import walletMixin from '~/mixins/wallet';
const MAX_NFT_CLASS_COUNT_PER_OWNER = 2;
const NFT_CLASS_DISPLAY_COUNT = 10;
export default {
name: 'WritingNFTPage',
layout: 'default',
Expand Down Expand Up @@ -213,17 +220,47 @@ export default {
this.$axios.$get(getTopNFTClasses()),
this.$axios.$get(getLatestNFTClasses()),
]);
[this.trendingClassIds, this.latestClassIds] = [trendingRes, latestRes].map(
res =>
(res.classes || [])
.filter(
c => c.metadata?.nft_meta_collection_id === 'likerland_writing_nft'
)
.map(c => c.id)
.slice(0, 10)
const [trendingClasses, latestClasses] = [trendingRes, latestRes].map(res =>
(res.classes || []).filter(
c => c.metadata?.nft_meta_collection_id === 'likerland_writing_nft'
)
);
this.latestClassIds = latestClasses
.slice(0, NFT_CLASS_DISPLAY_COUNT)
.map(c => c.id);
this.trendingClassIds = await this.filterNFTClassesByOwner(trendingClasses);
},
methods: {
async getClassOwner(classData) {
try {
const iscnPrefix = classData.parent.iscn_id_prefix;
const data = await this.$axios.$get(getISCNRecord(iscnPrefix));
return data.owner;
} catch (err) {
console.error(`Failed to fetch owner of ${classData.id}`);
return null;
}
},
async filterNFTClassesByOwner(nftClasses) {
const ownerList = await Promise.all(
// use 3 times of display count to ensure enough classes
nftClasses.slice(0, NFT_CLASS_DISPLAY_COUNT * 3).map(this.getClassOwner)
);
const filteredNFTClasses = [];
const ownerToNFTClassCountMap = {};
for (let i = 0; i < ownerList.length; i += 1) {
const owner = ownerList[i];
if (!ownerToNFTClassCountMap[owner]) {
ownerToNFTClassCountMap[owner] = 0;
}
if (ownerToNFTClassCountMap[owner] < MAX_NFT_CLASS_COUNT_PER_OWNER) {
filteredNFTClasses.push(nftClasses[i].id);
ownerToNFTClassCountMap[owner] += 1;
}
if (filteredNFTClasses.length >= NFT_CLASS_DISPLAY_COUNT) break;
}
return filteredNFTClasses;
},
handleTabClick(tab) {
const { query } = this.$route;
this.$router.replace({
Expand Down
33 changes: 28 additions & 5 deletions src/server/api/routes/users/v2/email.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const {
db,
FieldValue,
walletUserCollection,
nftMintSubscriptionCollection,
} = require('../../../../modules/firebase');
const {
authenticateV2Login,
Expand Down Expand Up @@ -93,22 +94,44 @@ router.put('/:wallet/email', async (req, res, next) => {
res.status(400).send('MISSING_TOKEN');
return;
}
await db.runTransaction(async t => {
const userRef = walletUserCollection.doc(user);

const userRef = walletUserCollection.doc(user);
const email = await db.runTransaction(async t => {
const userDoc = await t.get(userRef);
if (!userDoc.exists) {
throw new Error('USER_NOT_FOUND');
}
const { emailUnconfirmed, emailVerifyToken } = userDoc.data();
const { emailUnconfirmed: email, emailVerifyToken } = userDoc.data();
if (emailVerifyToken !== token) {
throw new Error('INVALID_TOKEN');
}
await t.update(userRef, {
email: emailUnconfirmed,
t.update(userRef, {
email,
emailUnconfirmed: FieldValue.delete(),
emailVerifyToken: FieldValue.delete(),
});
return email;
});

const legacyQuery = nftMintSubscriptionCollection
.where('subscriberEmail', '==', email)
.limit(499);
let querySnapshot = await legacyQuery.get();
while (querySnapshot.size) {
const batch = db.batch();
const legacyFollowees = querySnapshot.docs.map(
doc => doc.data().subscribedWallet
);
batch.update(userRef, {
followees: FieldValue.arrayUnion(...legacyFollowees),
});
querySnapshot.docs.forEach(doc => batch.delete(doc.ref));
// eslint-disable-next-line no-await-in-loop
await batch.commit();
// eslint-disable-next-line no-await-in-loop
querySnapshot = await legacyQuery.get();
}

res.sendStatus(200);
} catch (error) {
switch (error.message) {
Expand Down
42 changes: 30 additions & 12 deletions src/server/api/routes/users/v2/follow.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const {
db,
FieldValue,
walletUserCollection,
nftMintSubscriptionCollection,
} = require('../../../../modules/firebase');

const router = Router();
Expand All @@ -26,7 +27,18 @@ router.get(
res.json({ followees: [] });
return;
}
const { followees = [] } = userDoc.data();
const { followees: walletFollowees = [], email } = userDoc.data();
const snapshot = await nftMintSubscriptionCollection
.where('subscriberEmail', '==', email)
.get();
const legacyFollowees = snapshot.docs.map(doc => {
const { subscribedWallet } = doc.data();
return subscribedWallet;
});
const followees = new Set([
...walletFollowees,
...legacyFollowees,
]).values();
res.json({ followees });
} catch (err) {
handleRestfulError(req, res, next, err);
Expand Down Expand Up @@ -58,8 +70,7 @@ router.post(
throw new Error('EMAIL_NOT_SET_YET');
}
}
const creatorRef = walletUserCollection.doc(user);
await t.update(creatorRef, {
await t.update(userRef, {
followees: FieldValue.arrayUnion(creator),
});
});
Expand Down Expand Up @@ -92,18 +103,25 @@ router.delete(
res.status(400).send('INVALID_CREATOR_ADDRESS');
return;
}
await walletUserCollection.doc(user).update({
followees: FieldValue.arrayRemove(creator),
await db.runTransaction(async t => {
const userDoc = await t.get(walletUserCollection.doc(user));
const { email } = userDoc.data();
const snapshot = await t.get(
nftMintSubscriptionCollection
.where('subscriberEmail', '==', email)
.where('subscribedWallet', '==', creator)
.limit(1)
);
t.update(walletUserCollection.doc(user), {
followees: FieldValue.arrayRemove(creator),
});
if (snapshot.docs.length > 0) {
t.delete(snapshot.docs[0].ref);
}
});
res.sendStatus(200);
} catch (err) {
switch (err.message) {
case 'EMAIL_NOT_SET_YET':
res.status(403).send('EMAIL_NOT_SET_YET');
break;
default:
handleRestfulError(req, res, next, err);
}
handleRestfulError(req, res, next, err);
}
}
);
Expand Down

0 comments on commit 97b2cb5

Please sign in to comment.